How do I include Django 1.2's CSRF token in a Javascript-generated HTML form?

前端 未结 6 1692
感动是毒
感动是毒 2020-12-09 05:45

I recently upgraded to Django 1.2.3 and my upload forms are now broken. Whenever I attempt to upload, I receive a \"CSRF verification failed. Request aborted.\" error messag

6条回答
  •  粉色の甜心
    2020-12-09 06:18

    A better solution is to generate the code of the js file from a view/template.

    Then, in the view you can set the csrf token up in the context like so...

    from django.core.context_processors import csrf
    context = RequestContext(request)
    context.update(csrf(request))
    

    Then, in the template, you can use {{ csrf_token }} to get the raw value of the csrf token, and then use that to build a hidden field into your form with the name csrfmiddlewaretoken.

提交回复
热议问题