python 尝试使用文件浏览器为 Django 上传文件或创建文件夹时出现“CSRF 验证失败”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1718343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"CSRF verification failed" when attempting to upload a file or create a folder with filebrowser for Django
提问by Peter Horne
I have installed filebrowser for Django (not filebrowser3) and when I try to upload a file I recieve the following error:
我已经为 Django(不是 filebrowser3)安装了文件浏览器,当我尝试上传文件时,我收到以下错误:
403 Forbidden
CSRF verification failed. Request aborted.
More information is available with DEBUG=True.
403 禁地
CSRF 验证失败。请求中止。
更多信息可通过 DEBUG=True 获得。
The same error occurs when I try to create a new folder which shows that the problem is that filebrowser cannot create files/direcories within my uploads directory. I am using the default settings and have manually created the /media/uploads
directory with it's permissions set as 755
.
当我尝试创建一个新文件夹时出现同样的错误,这表明问题是文件浏览器无法在我的上传目录中创建文件/目录。我使用默认设置并手动创建了/media/uploads
目录,其权限设置为755
.
If I upload a file to the directory then file-browser admin page states:
如果我将文件上传到目录,则文件浏览器管理页面会显示:
1 Item Folder: 0 Image: 1
1 项目文件夹:0 图片:1
I cannot view the image that I have uploaded.
我无法查看我上传的图像。
I have the PIL and sorl.thumbnail modules installed.
我安装了 PIL 和 sorl.thumbnail 模块。
采纳答案by Daniel Roseman
You are probably using the development version of Django, which includes quite a lot of extra CRSF security. However it's not released yet so external products probably aren't compatible. You should use the 1.1 version of Django instead.
您可能正在使用 Django 的开发版本,其中包含相当多的额外 CRSF 安全性。但是它尚未发布,因此外部产品可能不兼容。您应该改用 1.1 版本的 Django。
回答by fitzgeraldsteele
The Djangodocs have more information about the new Django CSRF requirements:
Djangodocs 有关于新的Django CSRF 要求的更多信息:
回答by Gleb Svechnikov
Add to you settings.py this 2 lines:
将这 2 行添加到 settings.py 中:
‘django.middleware.csrf.CsrfViewMiddleware', ‘django.middleware.csrf.CsrfResponseMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfResponseMiddleware',
回答by daniel rodrigues
See here: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/like fitzgeralsteele said. Hugs.
请参阅此处:http: //docs.djangoproject.com/en/dev/ref/contrib/csrf/就像 fitzgeralsteele 所说。拥抱。
Example: from django.views.decorators.csrf import csrf_exempt
示例:从 django.views.decorators.csrf 导入 csrf_exempt
@csrf_exempt def my_view(request): return HttpResponse(‘Hello world')
@csrf_exempt def my_view(request): return HttpResponse('Hello world')
Disable the CSRF middleware.
禁用 CSRF 中间件。
回答by elin3t
it work adding
它工作添加
{% csrf_token %}
my forms looks like this:
我的表格是这样的:
<form method="post" action="">
{% csrf_token %}
{{ form.non_field_errors }}
{{formulario}}
<input type="submit" value="Guardar">
</form>
in to the tags on your template file and verify that 'django.middleware.csrf.CsrfViewMiddleware', is in your settings file, it is in by default
进入模板文件上的标签并验证“django.middleware.csrf.CsrfViewMiddleware”是否在您的设置文件中,默认情况下在