在 genericView 中设置当前相册 'pk'

Set up current Album 'pk' in genericView

我正在尝试从 Album 中删除 object (foto),我希望从 views 到 return 到 detail view(当前专辑),但我不能t 设置当前相册的pk。我尝试了各种方法,但我无法弄清楚并解决它。 也许我应该为照片的对象添加一些独立的 foto_pk,或者使用一些常用的定义来代替通用的 DeleteView?我没主意了。请给我一些线索...谢谢。

网址:

url(r'^$', views.IndexView.as_view(), name='index'),

url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),

url(r'albumadd/$', views.AlbumCreate.as_view(), name='album-add'),

url(r'update/(?P<pk>[0-9]+)/$', views.AlbumUpdate.as_view(), name='albumbum-update'),

url(r'delete/(?P<pk>[0-9]+)/$', views.AlbumDelete.as_view(), name='albumbum-delete'),

url(r'^(?P<pk>[0-9]+)/fotoadd/$', views.FotoCreate.as_view(), name='foto-add'),

url(r'^(?P<pk>[0-9]+)/fotodelete/$', views.FotoDelete.as_view(), name='foto-delete'),

观看次数:

class FotoDelete(LoginRequiredMixin, DeleteView):
    model = Foto

    def album(request, pk):
        album = Album.objects.get(pk=pk)
        return reverse_lazy('albumbum:detail', kwargs={'pk': album.pk})

其他尝试浏览量:

class FotoDelete(LoginRequiredMixin, DeleteView):
    model = Foto
    success_url = reverse_lazy('albumbum:detail', kwargs={'pk': Album.pk})

其他尝试浏览量:

class FotoDelete(LoginRequiredMixin, DeleteView):
    model = Foto
    album = get_object_or_404(Album, pk=pk)
    success_url = reverse_lazy('albumbum:detail', kwargs={'pk': album.pk})

我还附加了照片的 CreateView(相册中的对象): (也许这里是解决这个问题的东西)

class FotoCreate(CreateView):
    model = Foto
    fields = ['nazwa_foto', 'obraz']

    def form_valid(self, form):
        f = Album.objects.get(pk=self.kwargs.get('pk'))
        form.instance.albumnr = f
        return super(FotoCreate, self).form_valid(form)

    def get_success_url(self):
        return reverse('albumbum:detail', kwargs={'pk': self.kwargs.get('pk')})

models.py

class Album(models.Model):
    user = models.ForeignKey(User)
    nazwa_albumu = models.CharField(max_length=500, default="nazwa Albumu")
    temat = models.CharField(max_length=100, default="temat")
    opis_albumu = models.TextField(max_length=2000, default="opis")
    album_logo = models.CharField(max_length=1000, default="link http:// do logo")

    def get_absolute_url(self):
        return reverse('albumbum:detail', kwargs={'pk': self.pk})

    def __str__(self):
        return self.nazwa_albumu + ' - ' + str(self.user)


class Foto(models.Model):
    albumnr = models.ForeignKey(Album, on_delete=models.CASCADE)
    nazwa_foto = models.CharField(max_length=250, default='nazawa foto')
    obraz = models.CharField(max_length=1000, default='link http:// do obrazu')

    # def get_absolute_url(self):
    #     return reverse('albumbum:detail', kwargs={'pk': self.pk})

    def __str__(self):
        return self.nazwa_foto

试试这个,它应该有效。

class FotoDelete(LoginRequiredMixin, DeleteView):
    model = Foto

    def get_success_url(self):
         fotoid = self.kwargs['pk']
         foto = get_object_or_404(Foto, id=fotoid)
         album_id = foto.albumnr.pk
         return reverse_lazy('albumbum:detail', kwargs={'pk': album_id})

这也有效:

class FotoDelete(DeleteView):
    model = Foto

    def get_success_url(self):
        fotoid = self.kwargs['pk']
        foto = get_object_or_404(Foto, pk=fotoid)
        album_id = foto.albumnr.pk
        return reverse('albumbum:detail', kwargs={'pk': album_id})