Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python (Django)和#x27;评论视图';对象没有属性';车身';_Python_Django_Httpie - Fatal编程技术网

Python (Django)和#x27;评论视图';对象没有属性';车身';

Python (Django)和#x27;评论视图';对象没有属性';车身';,python,django,httpie,Python,Django,Httpie,我一直在尝试用一个装饰器来模拟注释功能 import json import jwt from django.views import View from django.http import JsonResponse from functools import wraps from django.db.models import Q from .models import

我一直在尝试用一个装饰器来模拟注释功能

import json
import jwt

from django.views           import View
from django.http            import JsonResponse
from functools              import wraps
from django.db.models       import Q

from .models                    import Comment
from account.models   import Account

class CommentView(View):


    def login_required(func):
        @wraps(func)
        def wrapper(request, *args, **kwargs):
            # import pdb; pdb.set_trace()
            given_token     = json.loads(request.body)['access_token']
            decoded_token   = jwt.decode(given_token,None,None)
            try:
                if Account.objects.filter(username=decoded_token).exists():
                    return func(request, *args, **kwargs)
                return JsonResponse({"message": "username does not exist"})
            except KeyError:
                return JsonResponse({"message": "INVALID_KEYS"}, status=403)
        return wrapper



    @login_required
    def post(self, request):
        print("request ", json.loads(request.body))
        data = json.loads(request.body)
        Comment.objects.create(
                username    = jwt.decode(json.loads(request.body)['access_token']),
                content     = data['content'],
        )
        return JsonResponse({"message":"Comment Created!"}, status=200)

    def get(self, request):
        return JsonResponse({'comment':list(Comment.objects.values())}, status=200)
我使用名为Httpie的程序发出JSON POST请求,如下所示:

http -v http://127.0.0.1:8000/comment access_token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImJlY2sifQ.2unop67pLHOshcGs385GwOvaZZW_J--TRNXyHI3gKNU" content="hello"
令牌没有问题,因为这是在SignenView(在另一个应用程序中)期间给出的令牌的精确副本

下面是“评论”应用程序中的models.py文件

from django.db                  import models
from account.models   import Account

class Comment(models.Model):
    username    = models.ForeignKey(Account, on_delete=models.CASCADE)
    content     = models.TextField()
    created_time= models.DateTimeField(auto_now_add = True)
    updated_time= models.DateTimeField(auto_now = True)

    class Meta:
        db_table = 'comments'

    def __str__(self):
        return self.username + ": " + self.content
但是,当我像上面那样使用Httpie发送POST请求时,我得到以下错误:

Internal Server Error: /comment
Traceback (most recent call last):
  File "/Users/woohyunan/miniconda3/auth/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/woohyunan/miniconda3/auth/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/woohyunan/miniconda3/auth/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/woohyunan/miniconda3/auth/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/woohyunan/miniconda3/auth/lib/python3.8/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/woohyunan/projects/Wecode/westagram/KillaGramz-backend/comment/views.py", line 19, in wrapper
    given_token     = json.loads(request.body)['access_token']
AttributeError: 'CommentView' object has no attribute 'body'
[20/May/2020 17:35:40] "POST /comment HTTP/1.1" 500 73224
我一直在想是什么导致了这个错误。我想知道是否没有办法将json请求主体放入decorator,这将允许我解码令牌(解码的版本将是用户名),以便查看它是否与数据库中的用户名匹配

非常感谢你

我解决了这个问题

def wrapper(request, *args, **kwargs):
需要

def wrapper(self, request, *args, **kwargs):