gpt4 book ai didi

javascript - 探戈与 django "Like Button"不工作

转载 作者:行者123 更新时间:2023-11-28 04:44:39 24 4
gpt4 key购买 nike

我遵循了本教程 http://www.tangowithdjango.com/book17/chapters/ajax.html#add-a-like-button但点击后我的点赞数并没有增加。

这是我的代码

View .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)

url.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

post_deutil.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

谢谢

最佳答案

在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_id”而不是“post.pk”进行 GET 调用

关于javascript - 探戈与 django "Like Button"不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43489401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com