在哪里/如何替换Django CBV中的默认上传处理程序? [英] Where/how to replace default upload handler in Django CBV?

查看:41
本文介绍了在哪里/如何替换Django CBV中的默认上传处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为基于类的视图指定一种处理文件上传的特定方法.根据 docs 可以实现通过类似的东西:

I am trying to specify a specific method of handling file uploads for a class based view. Per the docs this can be achieved by something like:

from django.core.files.uploadhandler import TemporaryFileUploadHandler
request.upload_handlers = [TemporaryFileUploadHandler(request=request)]

如果我在 FormView post 方法中指定此代码,则如下所示:

If i specify this in post method of a FormView like so:

def post(self, request, *args, **kwargs):
    request.upload_handlers = [TemporaryFileUploadHandler(request=request)]
    return super().post(self,  request, *args, **kwargs)

我得到:

AttributeError: You cannot set the upload handlers after the upload has been processed.

像这样的变量会产生相同的结果:

Variants like yield the same result:

def post(self, request, *args, **kwargs):
    self.request.upload_handlers = [TemporaryFileUploadHandler(request=self.request)]
    form = self.get_form()
    if form.is_valid():
        return self.form_valid(form)
    else:
        return self.form_invalid(form)

但是,当我在 get 方法中执行此操作时,此方法无效:

However when i do this in the get method this is ineffective:

 def get(self, request, *args, **kwargs):
    request.upload_handlers = [TemporaryFileUploadHandler(request=self.request)]  
    return super().get(self,  request, *args, **kwargs)

如果我上传一个小文件,它仍然使用默认的 django.core.files.uploadhandler.MemoryFileUploadHandler .

If I upload a small file it still uses the default django.core.files.uploadhandler.MemoryFileUploadHandler.

我在做什么错了?

此外,当我尝试镜像 note ,我得到相同的 AttributeError :

Also when i try to mirror what is suggested in the note, I get the same AttributeError:

from django.views.decorators.csrf import csrf_exempt, csrf_protect

@csrf_exempt
def post(self, request, *args, **kwargs):
    request.upload_handlers = [TemporaryFileUploadHandler(request=request)]
    return self._post(request, *args, **kwargs)

@csrf_protect
def _post(self, request, *args, **kwargs):
    form = self.get_form()
    if form.is_valid():
        return self.form_valid(form)
    else:
        return self.form_invalid(form)

推荐答案

好,终于成功了(使用@Alasdair提供的建议).在 post 上设置 method decorator(crsf_exempt)不够,需要在 dispatch 上进行设置.对于将来在此方面挣扎的任何人,它都是这样的:

Ok, finally got it to work (using the suggestions provided by @Alasdair). Setting a method decorator(crsf_exempt) on post is not engough it needs to be on dispatch. For anyone struggling with this in the future, it goes like this:

from django.views.generic import FormView
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt, csrf_protect


@method_decorator(csrf_exempt, 'dispatch')
class UploadDataSetView(FormView):

    def post(self, request, *args, **kwargs):
        request.upload_handlers = [TemporaryFileUploadHandler(request=request)]  
        return self._post(request)

    @method_decorator(csrf_protect)
    def _post(self, request):
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

如果您从模板中删除 {%csrf_token%} (这也是您想要的),也会失败.

Also it will fail if you remove the {% csrf_token %} from your template (which is what you want).

这篇关于在哪里/如何替换Django CBV中的默认上传处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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