我是django新手,我想在帖子上做一个类似的按钮,但我有这个错误。谢谢您!在
'WSGIRequest' object has no attribute 'Post'
这是我的Post模型:
class Post(models.Model):
created_date = models.DateTimeField()
title = models.CharField(max_length=100)
profile_image = models.ImageField(upload_to='poze', blank=True, null=True)
text = models.CharField(max_length=1000, default='Nimic', blank=True)
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
likes=models.ManyToManyField(UserProfile,related_name='likes',blank=True )
这是我的html模板:
^{pr2}$网址:
url(r'^like/', login_required(views.LikePost), name='like_post'),
视图:
def LikePost(request):
post=get_object_or_404(Post,id=request.Post.get('post_id'))
post.likes.add(request.user)
return HttpResponseRedirect(post.get_absolute_url())
控制台:
System check identified no issues (0 silenced). August 12, 2018 - 16:28:05 Django version 2.0.7, using settings 'DjangoApp.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. Internal Server Error: /account/like/ Traceback (most recent call last): File "C:\Users\Robbi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 35, in inner response = get_response(request) File "C:\Users\Robbi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\Robbi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\Robbi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view return view_func(request, *args, **kwargs) File "C:\Users\Robbi\PycharmProjects\DjangoApp\account\views.py", line 198, in LikePost post=get_object_or_404(Post,id=request.Post.get('post_id')) AttributeError: 'WSGIRequest' object has no attribute 'Post' [12/Aug/2018 16:46:10] "POST /account/like/ HTTP/1.1" 500 72571
从Django HttpRequest文档中,我们可以看到请求对象没有属性} 有。在
Post
,但是^{因此,使用
request.POST
而不是request.Post
^{pr2}$因此你的观点是,
Python是区分大小写的语言。} 。将
HttpRequest
没有Post
字段,而是有^{request.Post.get(...)
替换为request.POST.get(...)
相关问题 更多 >
编程相关推荐