在 Django 社交媒体网络中创建一个喜欢的按钮
Creating a like button in django social media network
我是 django 的新手,我正在尝试为我的博文创建一个赞按钮。在我的 HomeFeed 应用程序中
但是,当我点击我创建的“赞”按钮时收到此错误消息:
ValueError invalid literal for int() with base 10: 'uien-jdhn-fds'
我得到了一个提示,指出问题来自以下语句:
post = get_object_or_404(BlogPost, id=request.POST.get('blog_post_slug'))
是否因为他们期望 url 中的所有整数但它正在获取字符值?我不确定如何更改它。
Views.py
def LikeView(request, slug):
context = {}
post = get_object_or_404(BlogPost, id=request.POST.get('blog_post_slug'))
if post.likes.filter(id=request.user.id).exists():
post.likes.remove(request.user)
else:
post.likes.add(request.user)
return HttpResponseRedirect(reverse('HomeFeed:detail',args=[str(BlogPost.slug)]))
def detail_blog_view(request, slug):
context = {}
#need to import a package get_object_or_404. return object or throw 404
blog_post = get_object_or_404(BlogPost, slug=slug)
total_likes = blog_post.total_likes()
context['blog_post'] = blog_post
context['total_likes'] = total_likes
return render(request, 'HomeFeed/detail_blog.html', context)
models.py
class BlogPost(models.Model):
chief_title = models.CharField(max_length=50, null=False, blank=False)
body = models.TextField(max_length=5000, null=False, blank=False)
likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='blog_posts', blank=True)
slug = models.SlugField(blank=True, unique=True)
def __str__(self):
return self.chief_title
def total_likes(self):
return self.likes.count()
urls.py
from .views import(
detail_blog_view,
LikeView,
)
urlpatterns = [
path('<slug>/detail/', detail_blog_view, name= "detail"),
path('<slug>/edit/', edit_blog_view, name= "edit"),
path('<slug>/like/', LikeView, name='like_post'),
]
detail_blog.html
<form action="{% url 'HomeFeed:like_post' blog_post.slug %}" method="POST" >{% csrf_token %} <button type="submit" name="blog_post_slug" value="{{blog_post.slug}}" class='btn btn-primary btn-sm'>Like</button> {{ total_likes }} Likes</form>
回溯如下:
Internal Server Error: /HomeFeed/uien-jdhn-fds/like/
Traceback (most recent call last):
File "lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "views.py", line 195, in LikeView
post = get_object_or_404(BlogPost, id=request.POST.get('blog_post_slug'))
File "/lib/python3.8/site-packages/django/shortcuts.py", line 93, in get_object_or_404
return queryset.get(*args, **kwargs)
File "lib/python3.8/site-packages/django/db/models/query.py", line 399, in get
clone = self.filter(*args, **kwargs)
File "/lib/python3.8/site-packages/django/db/models/query.py", line 892, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "lib/python3.8/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1290, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1315, in _add_q
child_clause, needed_inner = self.build_filter(
File "/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1251, in build_filter
condition = self.build_lookup(lookups, col, value)
File "lib/python3.8/site-packages/django/db/models/sql/query.py", line 1116, in build_lookup
lookup = lookup_class(lhs, rhs)
File "lib/python3.8/site-packages/django/db/models/lookups.py", line 20, in __init__
self.rhs = self.get_prep_lookup()
File "/lib/python3.8/site-packages/django/db/models/lookups.py", line 70, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 972, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'uien-jdhn-fds'
您提交了 slug,而不是主键,因此:
def LikeView(request, slug):
context = {}
post = get_object_or_404(BlogPost, <b>slug=</b>request.POST.get('blog_post_slug'))
# …
然而不需要通过按钮提交,您已经通过路径提交了,所以您可以使用的:
def LikeView(request<b>, slug</b>):
context = {}
post = get_object_or_404(BlogPost, <b>slug=slug</b>)
# …
此外,你还应该使用post.slug
,或者在反转时直接使用slug
,所以:
from django.shortcuts import <b>redirect</b>
def LikeView(request, slug):
# …
return redirect('HomeFeed:detail'<b>, slug=slug</b>)
我是 django 的新手,我正在尝试为我的博文创建一个赞按钮。在我的 HomeFeed 应用程序中
但是,当我点击我创建的“赞”按钮时收到此错误消息:
ValueError invalid literal for int() with base 10: 'uien-jdhn-fds'
我得到了一个提示,指出问题来自以下语句:
post = get_object_or_404(BlogPost, id=request.POST.get('blog_post_slug'))
是否因为他们期望 url 中的所有整数但它正在获取字符值?我不确定如何更改它。
Views.py
def LikeView(request, slug):
context = {}
post = get_object_or_404(BlogPost, id=request.POST.get('blog_post_slug'))
if post.likes.filter(id=request.user.id).exists():
post.likes.remove(request.user)
else:
post.likes.add(request.user)
return HttpResponseRedirect(reverse('HomeFeed:detail',args=[str(BlogPost.slug)]))
def detail_blog_view(request, slug):
context = {}
#need to import a package get_object_or_404. return object or throw 404
blog_post = get_object_or_404(BlogPost, slug=slug)
total_likes = blog_post.total_likes()
context['blog_post'] = blog_post
context['total_likes'] = total_likes
return render(request, 'HomeFeed/detail_blog.html', context)
models.py
class BlogPost(models.Model):
chief_title = models.CharField(max_length=50, null=False, blank=False)
body = models.TextField(max_length=5000, null=False, blank=False)
likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='blog_posts', blank=True)
slug = models.SlugField(blank=True, unique=True)
def __str__(self):
return self.chief_title
def total_likes(self):
return self.likes.count()
urls.py
from .views import(
detail_blog_view,
LikeView,
)
urlpatterns = [
path('<slug>/detail/', detail_blog_view, name= "detail"),
path('<slug>/edit/', edit_blog_view, name= "edit"),
path('<slug>/like/', LikeView, name='like_post'),
]
detail_blog.html
<form action="{% url 'HomeFeed:like_post' blog_post.slug %}" method="POST" >{% csrf_token %} <button type="submit" name="blog_post_slug" value="{{blog_post.slug}}" class='btn btn-primary btn-sm'>Like</button> {{ total_likes }} Likes</form>
回溯如下:
Internal Server Error: /HomeFeed/uien-jdhn-fds/like/
Traceback (most recent call last):
File "lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "views.py", line 195, in LikeView
post = get_object_or_404(BlogPost, id=request.POST.get('blog_post_slug'))
File "/lib/python3.8/site-packages/django/shortcuts.py", line 93, in get_object_or_404
return queryset.get(*args, **kwargs)
File "lib/python3.8/site-packages/django/db/models/query.py", line 399, in get
clone = self.filter(*args, **kwargs)
File "/lib/python3.8/site-packages/django/db/models/query.py", line 892, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "lib/python3.8/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1290, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1315, in _add_q
child_clause, needed_inner = self.build_filter(
File "/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1251, in build_filter
condition = self.build_lookup(lookups, col, value)
File "lib/python3.8/site-packages/django/db/models/sql/query.py", line 1116, in build_lookup
lookup = lookup_class(lhs, rhs)
File "lib/python3.8/site-packages/django/db/models/lookups.py", line 20, in __init__
self.rhs = self.get_prep_lookup()
File "/lib/python3.8/site-packages/django/db/models/lookups.py", line 70, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 972, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'uien-jdhn-fds'
您提交了 slug,而不是主键,因此:
def LikeView(request, slug):
context = {}
post = get_object_or_404(BlogPost, <b>slug=</b>request.POST.get('blog_post_slug'))
# …
然而不需要通过按钮提交,您已经通过路径提交了,所以您可以使用的:
def LikeView(request<b>, slug</b>):
context = {}
post = get_object_or_404(BlogPost, <b>slug=slug</b>)
# …
此外,你还应该使用post.slug
,或者在反转时直接使用slug
,所以:
from django.shortcuts import <b>redirect</b>
def LikeView(request, slug):
# …
return redirect('HomeFeed:detail'<b>, slug=slug</b>)