Javascript 探戈舞曲与django“;“喜欢按钮”;不起作用
我遵循了这个教程 但我的like计数在单击后不会增加 这是我的密码 views.pyJavascript 探戈舞曲与django“;“喜欢按钮”;不起作用,javascript,jquery,python,ajax,django,Javascript,Jquery,Python,Ajax,Django,我遵循了这个教程 但我的like计数在单击后不会增加 这是我的密码 views.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
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)
url.py
url(r'like_post/$', views.like_post, name='like_post'),
Models.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
post_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 %}
控制台消息
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
谢谢您在views.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']
这是因为在blog-ajax.js中,您使用参数“post\u id”而不是views.py中的“post.pk”进行GET调用使用:
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']
这是因为在blog-ajax.js中,您使用参数“post\u id”而不是“post.pk”进行GET调用。在终端内部服务器错误:/blog/like\u post/Traceback(最后一次调用):File”/home/bharat/.local/lib/python3.5/site packages/django/utils/datastructures.py),第83行,在getitem list中uu=super(多值dict,self)。uu getitem_u(key)keyrerror:“post_id”post_id从未在blog-ajax.js中获取值。您需要更改
var postid;catid=$(this.attr(“数据发布id”)代码>至var postid;postid=$(this.attr(“数据post_id”)代码>这样它就可以填充正确的变量谢谢,它可以工作,但是如何添加厌恶按钮呢?我的意思是撤销它。在终端内部服务器错误:/blog/like_post/Traceback(最后一次调用):File“/home/bharat/.local/lib/python3.5/site packages/django/utils/datastructures.py”,第83行,在getitem list=super(MultiValueDict,self)中KeyError:“post_id”post_id从未在blog-ajax.js中获取值。您需要更改var postid;catid=$(this.attr(“数据发布id”)代码>至var postid;postid=$(this.attr(“数据post_id”)代码>这样它就可以填充正确的变量谢谢,它可以工作,但是如何添加厌恶按钮呢?我的意思是撤销它。