3

I`m trying to upload files to Django with SWFUpload. Found this article Django with SWFUpload. 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). Is there a better way to handle this rather than turning off csrf?

I know that i can pass custom data with SWFUpload post_params: {"csrfmiddlewaretoken" : ""},. But i don`t know how to get only value of csrf token in template, not a full input tag.

4

1 に答える 1

2

csrf トークン自体を取得するには、Django の内部構造の一部を使用する必要があります。まず、この行をビューの上部に含めます。

from django.middleware.csrf import get_token

テンプレートにパラメーターを渡すときは、次のようにします。

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

テンプレートでは、トークンを で参照するだけ{{ csrf_token }}です。

于 2010-07-04T00:10:19.950 に答える