Djangolike按钮没有更新likes

2024-04-25 10:06:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我即将完成探戈与Django教程,我接近主曲,正在制作一个“like”按钮:

http://www.tangowithdjango.com/book17/chapters/ajax.html#add-a-like-button 应该发生的是:

when the button is clicked an AJAX request is made, given our url mapping, this invokes the like_category view which updates the category and returns the new number of likes. When the AJAX request receives the response it updates parts of the page, i.e. the text and the button. The #likes button is hidden.

我的按钮在那里,可以点击,但没有隐藏后被点击,也没有实际增加的数量喜欢,我不知道为什么,我成功地创建了按钮在上一章介绍。在

这些是相关的代码片段(但是如果我遗漏了一些有用的东西,请告诉我:

视图.py

@login_required

def like_category(request):

    cat_id = None
    if request.method == 'GET':
        cat_id = request.GET['post_id']

    likes = 0
    if cat_id:
        cat = Post.objects.get(id=int(cat_id))
        if cat:
            likes = cat.likes + 1
            cat.likes =  likes
            cat.save()

    return HttpResponse(likes)

兰戈-阿贾克斯.js

^{pr2}$

关于.html

^{3}$

模型.py

class Post(models.Model):
    post = models.TextField()
    user = models.ForeignKey(User)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    likes = models.IntegerField(default=0)

    def __unicode__(self):
        return self.name
    def __str__(self):
        return self.post

这是我的控制台在单击“类似”按钮时显示的内容:

Not Found: /assets/js/ie10-viewport-bug-workaround.js [19/Apr/2017 06:55:29] "GET /assets/js/ie10-viewport-bug-workaround.js HTTP/1.1" 404 5180 [19/Apr/2017 06:55:29] "GET /static/fonts/glyphicons-halflings-regular.woff HTTP/1.1" 404 1730 [19/Apr/2017 06:55:29] "GET /static/fonts/glyphicons-halflings-regular.ttf HTTP/1.1" 404 1727

Tags: theselfidgetismodelsrequestdef