简体   繁体   English

SWFUpload与Django 1.2 csrf问题

[英]SWFUpload with Django 1.2 csrf problem

I`m trying to upload files to Django with SWFUpload. 我正在尝试使用SWFUpload将文件上传到Django。 Found this article Django with SWFUpload . 用SWFUpload发现这篇文章Django But found one problem. 但发现了一个问题。 In Django 1.2 a csrf requires a csrf token to be send on every form submit, and it includes files that are send with SWFUpload.So uploading doesnt until i turn off csrf ( globally or for view using @csrf_exempt decorator). 在Django 1.2中,csrf需要在每个表单提交时发送csrf令牌,并且它包含使用SWFUpload发送的文件。上传不会直到我关闭csrf(全局或使用@csrf_exempt装饰器进行查看)。 Is there a better way to handle this rather than turning off csrf? 有没有更好的方法来处理这个而不是关闭csrf?

I know that i can pass custom data with SWFUpload post_params: {"csrfmiddlewaretoken" : ""},. 我知道我可以使用SWFUpload post_params传递自定义数据:{“csrfmiddlewaretoken”:“”} ,. But i don`t know how to get only value of csrf token in template, not a full input tag. 但我不知道如何只在模板中获取csrf标记的值,而不是完整的输入标记。

To retrieve the csrf token itself, you'll need to resort to using some of Django's internals. 要检索csrf令牌本身,您需要使用一些Django的内部。 First off, include this line at the top of your view. 首先,在视图的顶部包含此行。

from django.middleware.csrf import get_token

Now, when passing parameters to your template, do something like 现在,在将参数传递给模板时,请执行类似的操作

def my_view(request):
    return render_to_response("index.html", {"csrf_token": get_token(request)})

In your template, just reference the token with {{ csrf_token }} . 在您的模板中,只需使用{{ csrf_token }}引用令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM