Csrf Verification Failing In Django/backbone.js
I'm trying to recreate a small project from lightweight django - https://github.com/lightweightdjango/examples/tree/chapter-5 I'm getting a CSRF error when trying to login with the
Solution 1:
It's not clear from the code sample if you are defining the CRSF token. If you are using django templates, you can set {% csrf_token %}
to be your CRSF
token, somewhere in your code.
Solution 2:
I had exactly the same issue. Then as suggested in page 111 of the book:
This assumes that the project is using the default cookie name
csrftoken
. If needed, this token could be configured via the configuration parsed by app.js.
I added "csrftoken": "{% csrf_token %}"
to the "config" section in index.html:
...
<scriptsrc="{% static 'board/vendor/backbone.js' %}"></script><scriptid="config"type="text/json">
{
"models": {},
"collections": {},
"views": {},
"router": null,
"csrftoken": "{% csrf_token %}", //added this"apiRoot": "{% url 'api-root' %}",
"apiLogin": "{% url 'api-token' %}"
}
</script><scriptsrc="{% static 'board/js/app.js' %}"></script>
...
With this change, the error was fixed and I was able to log in.
Post a Comment for "Csrf Verification Failing In Django/backbone.js"