Skip to main content

Enable CSRF protection only for HTML forms

Project description


The CSRF middleware and template tag from Django framework provides easy-to-use protection against Cross Site Request Forgeries. This protector has some inconveniences for XHR POST requests.

This module enables CSRF protection only for HTML forms when content type of the request is one of the following:

  • application/x-www-form-urlencoded

  • multipart/form-data

  • text/plain

It is generally safe to exclude XHR requests from CSRF protection, because XHR requests can only be made from the same origin. Check your CORS configuration before using this module. Use django-cors-headers module to protect your site with CORS.


Install with pip or pipenv:

pip install django-csrf-protect-form


You can set a list of content types which have CSRF protection enabled. The default value is:



from django_csrf_protect_form import csrf_protect_form

def hello(request):
    return HttpResponse("<html><head></head><body>Hello, world!</body></html>")


from django_csrf_protect_form import csrf_protect_form
from .views import hello

urlpatterns = [
    url('hello/', csrf_protect_form(hello)),




Copyright © 2019, Piotr Roszatycki

This software is distributed under the GNU Lesser General Public License (LGPL 3 or greater).

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-csrf-protect-form-0.1.0.tar.gz (16.8 kB view hashes)

Uploaded Source

Built Distribution

django_csrf_protect_form-0.1.0-py2.py3-none-any.whl (6.0 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page