Search code examples
pythondjangocsrfdjango-csrf

Is it possible to change the Django csrf token name and token header


I am building a app using Angular and Django

by default, Django uses X-CSRFToken as the csrf header and csrftoken as the token name.

I Want to rename the header name to something X-SOMENAME and token as sometokenName,

I know with Angular we can change the default names with$http.defaults

Is it possible to change the token name on Django so that the generated token has sometokenName and the header Django looks to X-SOMENAME?

Thank you.


Solution

  • For header name and cookie name you can change it using CSRF_COOKIE_NAME and CSRF_HEADER_NAME. Unfortunately, you can't change POST field that easy. You will have to modify CsrfViewMiddleware for that. But if you're using angular, you can use only headers and completely omit POST fields for that.