无法为django正确配置succes_url

时间:2017-06-08 20:54:58

标签: django primary-key

views.py

class SongDelete(DeleteView):
    model = Song
    success_url=reverse_lazy("music:detail", song)

models.py

class Album(models.Model):
    artist = models.CharField(max_length=250)
    album_title = models.CharField(max_length=500)
    genre = models.CharField(max_length=100)
    album_logo = models.FileField()

    def get_absolute_url(self):
        return reverse('music:detail', kwargs={'pk':self.pk})
    #return details of the album page we just created with the primary key

    def __str__(self):
        return self.album_title + " - " + self.artist
class Song(models.Model):
    album = models.ForeignKey(Album, on_delete=models.CASCADE)
    file_type = models.CharField(max_length=10)
    song_title = models.CharField(max_length=250)
    is_favorite = models.BooleanField(default=False)
    song_file = models.FileField(null=True)

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

    def __str__(self):
        return self.song_title

网址:

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

问题: 我想在从页面删除歌曲后重定向到详细信息视图音乐。我可以重定向到索引页面,但在重定向到具有主键的相册页面的详细信息时出现问题。我确定它是success_url中的reverse_lazy函数,需要修复。有什么建议吗?

0 个答案:

没有答案