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. Bootstrap Accordian by Netplay4 1 year, 9 months ago
  2. Bootstrap theme for django-endless-pagination? by se210 4 years, 9 months ago
  3. Bootstrap theme for django-endless-pagination? by se210 4 years, 9 months ago
  4. Reusable form template with generic view by roldandvg 4 years, 10 months ago
  5. Pagination Django with Boostrap by guilegarcia 5 years ago

Comments

mpf (on July 31, 2010):

Or you can use {{ csrf_token }} instead.

#

Please login first before commenting.