Skip to content

Add CSRF helpers to Django.js#9

Merged
noirbizarre merged 1 commit intonoirbizarre:masterfrom
rouge8:csrf-helpers
Feb 26, 2013
Merged

Add CSRF helpers to Django.js#9
noirbizarre merged 1 commit intonoirbizarre:masterfrom
rouge8:csrf-helpers

Conversation

@rouge8
Copy link
Contributor

@rouge8 rouge8 commented Feb 25, 2013

Django.csrf_token(); returns the value of the CSRF token.

Django.csrf_element(); returns an element containing the CSRF token,
as if you were to use {% csrf_token %}.

The primary purpose of this is for creating non-AJAX forms with Javascript rather than Django Forms.

@rouge8
Copy link
Contributor Author

rouge8 commented Feb 26, 2013

@cpdean added tests

@noirbizarre
Copy link
Owner

Interesting!

Right now the tests fails on Django 1.5 because the csrf_token template tag is no longer enclosed in a div.
See: https://docs.djangoproject.com/en/dev/releases/1.5/#miscellaneous

In Django 1.4:

<div style="display:none"><input type="hidden" name="csrfmiddlewaretoken" value="YYB0aBXCinye9jZbmkvkSWvc1zQ4aRjg"></div>

In Django 1.5:

<input type="hidden" name="csrfmiddlewaretoken" value="7GPRlZilh1swRp3VPtGzPdu3EyhYNtGk">

I think the important part is the input, not the outer div.

Why not change the test to only test the input:

var $csrfTag = $('#test-csrf-elem input[name="csrfmiddlewaretoken"]');

In the documentation, instead of saying "return the element containing the CSRF token, as if you'd used the {% csrf_token %} template tag", just say it render an hidden input like {% csrf_token %} would.

Just forget the outer div.

Fix it and rebase, I'll merge it ASAP (when tests passes).

@rouge8
Copy link
Contributor Author

rouge8 commented Feb 26, 2013

I didn't think to check the 1.5 docs, I figured there was a reason Django included the div in the token. Updated with the fix you suggested.

`Django.csrf_token();` returns the value of the CSRF token.

`Django.csrf_element();` returns an element containing the CSRF token,
as if you were to use `{% csrf_token %}`.
noirbizarre added a commit that referenced this pull request Feb 26, 2013
Add CSRF helpers to Django.js
@noirbizarre noirbizarre merged commit e27a408 into noirbizarre:master Feb 26, 2013
@noirbizarre
Copy link
Owner

Thanks

@rouge8 rouge8 deleted the csrf-helpers branch February 26, 2013 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants