Search code examples
djangocsrfjinja2

How to csrf_token protection in jinja2 template engine?


In Django template I used:

<form action="/user" method="post">{% csrf_token %}
    {{ form.as_p|safe }}
    <input type="submit" value="Submit" />
</form>

But error when I change to jinja2 template engine:

 Encountered unknown tag 'csrf_token'

My question: csrf_token protection in jinja2 is required?

If required, how to do this?

Thanks in advance!


Solution

  • It seems Jinja2 works differently:

    Use <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}"> where in Django templates you use {% csrf_token %}

    source : http://exyr.org/2010/Jinja-in-Django/