Login

Django csrf_token Template Tag Fix

Author:
Reustle
Posted:
July 31, 2010
Language:
HTML/template
Version:
1.2
Tags:
csrf_token csrf_token_clean
Score:
0 (after 0 ratings)

If you currently use {% csrf_token %}, you will notice it prints a hidden div, and an xHTML input tag. What if you don't want that hidden div, and/or you want your page to validate with HTML and not xHTML.

This snippet returns only the csrf token itself, and none of the related HTML code. You can use it like this.

<input type="hidden" name="csrfmiddlewaretoken" value="{% with csrf_token as csrf_token_clean %}{{ csrf_token_clean }}{% endwith %}" >

1
{% with csrf_token as csrf_token_clean %}{{ csrf_token_clean }}{% endwith %}

More like this

  1. csrf_token for mako by damd 5 years, 1 month ago
  2. Convert XHTML-compatible shorttag to HTML-compatible tag. by clamothe 7 years, 2 months ago
  3. really spaceless (trim spaces at line start) by wolfram 7 years, 5 months ago
  4. Add CSS class template filter by lazerscience 4 years, 7 months ago
  5. Boxes as template tags by pedrolima 7 years, 4 months ago

Comments

mpf (on July 31, 2010):

Or you can use {{ csrf_token }} instead.

#

Please login first before commenting.