Login

Django csrf_token Template Tag Fix

Author:
Reustle
Posted:
July 31, 2010
Language:
HTML/template
Version:
1.2
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. Bootstrap Accordian by Netplay4 5 years, 11 months ago
  2. Bootstrap theme for django-endless-pagination? by se210 8 years, 11 months ago
  3. Bootstrap theme for django-endless-pagination? by se210 8 years, 11 months ago
  4. Reusable form template with generic view by roldandvg 9 years ago
  5. Pagination Django with Boostrap by guilegarcia 9 years, 2 months ago

Comments

mpf (on July 31, 2010):

Or you can use {{ csrf_token }} instead.

#

Please login first before commenting.