Django URL调度错误-AttributeError:"WSGIRequest"对象没有属性"data" [英] Django url dispatch error - AttributeError: 'WSGIRequest' object has no attribute 'data'

查看:85
本文介绍了Django URL调度错误-AttributeError:"WSGIRequest"对象没有属性"data"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据类中方法的路径进行分派.当我必须将其传递给post方法时,就会出现问题.另外,我可以使用process_request方法将其包装在Request上.

I am dispatching based on the path to my methods in the class. The problem arises when I have to pass it to a post method. Else I can wrap it over a Request using the process_request method.

如何将正确的请求传递给方法我想这不是HttpRequest而是DRF 3 Request.

How do I pass the correct request to the methods which is not HttpRequest but the DRF 3 Request I suppose.

class AddInvoice(APIView):
    @staticmethod
    def process_request(request, *args, **kwargs):
        if isinstance(request, HttpRequest):
            return Request(request,parsers=[MultiPartParser, FormParser, JSONParser, DjangoMultiPartParser])
        return request

    parser_classes = (MultiPartParser, FormParser, JSONParser, DjangoMultiPartParser, FileUploadParser)

    def dispatch(self, request, *args, **kwargs):
        response = None
        #request = AddInvoice.process_request(request, *args, **kwargs)
        if request.method == 'PUT':
            if request.path.rstrip('/') == '/invoice/digitize':
                response = self.digitize(request,*args, **kwargs)
        elif request.method == 'GET':
            if request.path.startswith('/invoice/isdigitized/'):
                response = self.isdigitized(request, *args,
                                             **kwargs)
            elif request.path.startswith('/invoice/get/'):
                response = self.get(request, *args, **kwargs)
        elif request.method == 'POST':
            if request.path.rstrip('/') == '/invoice':
                response = self.post(request, *args, **kwargs)
        if not response:
            response = Response(status=status.HTTP_406_NOT_ACCEPTABLE)
        if not getattr(request, 'accepted_renderer', None):
            neg = self.perform_content_negotiation(request, force=True)
            request.accepted_renderer, request.accepted_media_type = neg
        response.accepted_renderer = request.accepted_renderer
        response.accepted_media_type = request.accepted_media_type
        response.renderer_context = self.get_renderer_context()
        return response

推荐答案

我使用了另一种方法来绕过创建APIView类作为对象并使用它的问题,方法是使用 add_invoice = AddInvoice()和获得替代解决方案.Django并不是很友善,没有其他选择可以将其转换为DRF 3请求.

I used some alternate way to bypass the issue creating a class of APIView as an object and using it add_invoice = AddInvoice() and get an alternate solution. Django hasn't been very kind there is no other alternative to convert this to DRF 3 Request.

@staticmethod
@api_view(["GET", 'PUT', "POST"])
def dispatch(request, *args, **kwargs):
    response = None
    add_invoice = AddInvoice()
    if request.method == 'PUT':
        if request.path.rstrip('/') == '/invoice/digitize':
            response = add_invoice.digitize(request, *args, **kwargs)
    elif request.method == 'GET':
        if request.path.startswith('/invoice/isdigitized/'):
            response = add_invoice.isdigitized(request, *args,
                                              **kwargs)
        elif request.path.startswith('/invoice/get/'):
            response = add_invoice.get(request, *args, **kwargs)
    elif request.method == 'POST':
        if request.path.rstrip('/') == '/invoice':
            response = add_invoice.post(request)

    if not response:
        response = Response(status=status.HTTP_406_NOT_ACCEPTABLE)
    if not getattr(request, 'accepted_renderer', None):
        neg = add_invoice.perform_content_negotiation(request, force=True)
        request.accepted_renderer, request.accepted_media_type = neg
    response.accepted_renderer = request.accepted_renderer
    response.accepted_media_type = request.accepted_media_type
    response.renderer_context = add_invoice.get_renderer_context()
    return response

这篇关于Django URL调度错误-AttributeError:"WSGIRequest"对象没有属性"data"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆