我遵循了本教程 http://www.tangowithdjango.com/book17/chapters/ajax.html#add-a-like-button 但点击它后我的数量并没有增加。
这是我的代码
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 post:
likes = post.likes + 1
post.likes = likes
post.save()
return HttpResponse(likes)
urls.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 %}
博客-ajax.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
谢谢
答案 0 :(得分:0)
在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']
这是因为在您的博客-ajax.js中,您使用参数&#39; post_id&#39;进行GET调用。而不是post.pk&#39;