SWFUpload with Django 1.2 csrf problem

I am trying to upload files to Django using SWFUpload. Found this Django article with SWFUpload . But found one problem. In Django 1.2, csrf requires the csrf token to be sent to each submit file and include files sent from SWFUpload. So the download is not until I turn off csrf (globally or for viewing with @csrf_exempt decorator). Is there a better way to handle this and not disable csrf?

I know that I can transfer user data using SWFUpload post_params: {"csrfmiddlewaretoken": ""}. But I do not know how to get only the csrf token value in the template, and not the full input tag.

+3
source share
1 answer

To extract the csrf token itself, you need to resort to using some of the internal components of Django. First, include this line at the top of your view.

from django.middleware.csrf import get_token

Now, passing the parameters to your template, do something like

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

In the template, just specify the token with {{ csrf_token }}.

+2
source

Source: https://habr.com/ru/post/1753008/


All Articles