Django“WSGIRequest”对象没有属性“Post”

2024-03-28 12:07:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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


Tags: djangoinpytruegetobjectmodelsresponse
2条回答

Django HttpRequest文档中,我们可以看到请求对象没有属性Post,但是^{}有。在

因此,使用request.POST而不是request.Post

post=get_object_or_404(Post,id=request.POST.get('post_id'))


因此你的观点是,

^{pr2}$

Python是区分大小写的语言。HttpRequest没有Post字段,而是有^{}。将request.Post.get(...)替换为request.POST.get(...)

相关问题 更多 >