django最喜欢的按钮

时间:2018-12-07 12:55:22

标签: django favorites

我想为网站的最终用户实现一个书签(收藏夹按钮),以便他们可以存储自己喜欢的冒险。

models.py

class Favorit(models.Model):
    related_adventure = models.ForeignKey(Adventure, on_delete=models.CASCADE, related_name='favorit')
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __unicode__(self):
                    return '%s %s'%(self.related_adventure.name, self.user.username)

views.py

@login_required

def favorit_adventure(request,adventure_pk):

            user = request.user
            adventure = Adventure.objects.get(pk=adventure_pk)
            if(Favorit.objects.get(user = request.user and related_adventure == adventure_pk)):
                    print ('geht')

            # If no new bookmark has been created,
            # Then we believe that the request was to delete the bookmark
            messages.success("Erfolgreich")
            if not created:
                    favorit.delete()
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

和.html这样

.html

想法是编写一个def并从当前模板中添加冒险,并给出pk(主键)。 问题是对视图进行适当的冒险。然后应创建一个新的实例收藏夹。

我的问题是单击按钮时没有任何动作。该怎么办?

预先感谢

0 个答案:

没有答案