与django一起跳探戈的"点赞按钮"不起作用

2024-10-17 08:29:27 发布

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

我遵循了这个教程 http://www.tangowithdjango.com/book17/chapters/ajax.html#add-a-like-button 但是我的like计数在单击它之后并没有增加。你知道吗

这是我的密码

你知道吗视图.py你知道吗

def like_post(request):

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

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

    return HttpResponse(likes)

你知道吗网址.py你知道吗

url(r'like_post/$', views.like_post, name='like_post'),

你知道吗型号.py你知道吗

class Post(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True,null=True)
    likes = models.IntegerField(default=0)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

岗位_deatil.html文件你知道吗

<strong id="like_count">{{ post.likes }}</strong> people like this category

{% if user.is_authenticated %}
        <button id="likes" data-post_id="{{post.id}}"  class="btn btn-primary" type="button">
        <span class="glyphicon glyphicon-thumbs-up"></span>
        Like
        </button>
{% endif %}

博客-阿贾克斯.js你知道吗

$('#likes').click(function(){
    var postid;
    catid = $(this).attr("data-post_id");
    $.get('/blog/like_post/', {post_id: postid}, function(data){
               $('#like_count').html(data);
               $('#likes').hide();
    });
});

控制台消息

Internal Server Error: /blog/like_post/
Traceback (most recent call last):
  File "/home/bharat/.local/lib/python3.5/site-packages/django/utils/datastructures.py", line 83, in __getitem__
    list_ = super(MultiValueDict, self).__getitem__(key)
KeyError: 'post.pk'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/bharat/.local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = get_response(request)
  File "/home/bharat/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/bharat/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/bharat/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/bharat/.local/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/bharat/Desktop/DiggingIntoDjango-TreasuregramSampleApp-master/my-first-blog-master/blog/views.py", line 103, in like_post
    post_id = request.GET['post.pk']
  File "/home/bharat/.local/lib/python3.5/site-packages/django/utils/datastructures.py", line 85, in __getitem__
    raise MultiValueDictKeyError(repr(key))
django.utils.datastructures.MultiValueDictKeyError: "'post.pk'"
Internal Server Error: /blog/like_post/
Traceback (most recent call last):
  File "/home/bharat/.local/lib/python3.5/site-packages/django/utils/datastructures.py", line 83, in __getitem__
    list_ = super(MultiValueDict, self).__getitem__(key)
KeyError: 'post.pk'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/bharat/.local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = get_response(request)
  File "/home/bharat/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/bharat/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/bharat/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/bharat/.local/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/bharat/Desktop/DiggingIntoDjango-TreasuregramSampleApp-master/my-first-blog-master/blog/views.py", line 103, in like_post
    post_id = request.GET['post.pk']
  File "/home/bharat/.local/lib/python3.5/site-packages/django/utils/datastructures.py", line 85, in __getitem__
    raise MultiValueDictKeyError(repr(key))
django.utils.datastructures.MultiValueDictKeyError: "'post.pk'"
[19/Apr/2017 13:03:36] "GET /blog/like_post/ HTTP/1.1" 500 16637
[19/Apr/2017 13:03:36] "GET /blog/like_post/ HTTP/1.1" 500 16637

This is how i get, but after clicking on like nothing happens

谢谢


Tags: djangoinpyhomeresponserequestlibpackages
1条回答
网友
1楼 · 发布于 2024-10-17 08:29:27

在视图.py用途:

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

而不是:

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

这是因为在你的博客里-阿贾克斯.js使用参数'post\u id'和'not'进行GET调用邮政pk'

相关问题 更多 >