Add auto csrf_token header setting to support ajax post requests

I have this history with django, that, when I try to post my forms with Ajax. It was a nightmare as a starter. (long ago.) I bet it’s the case for lot of developers. So many things to set manually.

  • put the template tag +
  • make an html element +
  • use js to pick the element +
  • set the header

Why can we automate this?
So, I was wondering, why isn’t there a auto header setting template tag?

Is it?

Tell me your idea about this. :rocket:

1 Like

Well, what if user just want to test the form data using AJAX or he used csrf_exempt then what about these cases??

If someone try suggested tag in his/her code, that means he is trying to make ajax work with csrf_token. So, there’s no conflict with csrf_exempt given that csrf_exempt is used to remove the protection layer made with csrf_token(in testing).
As I understand csrf_exempt is for testing only.

I don’t understand the problem statement and the suggested resolution. You already have access to the csrf_token template variable. What are you doing where this is not sufficient?

There’s no problem. Only a suggestion, is it worth it to create a template tag that sets ajax header automatically?. Maybe it’s a bad idea, because it’s ajax specific thing. I was just a suggestion.
Like we make a form in the site, it must be post ajax, now, suddenly you have to go through extra steps to configure every ajax post function.

I don’t think adding AJAX-specific functionality to Django is a good idea. It’s too specific of a use-case, which can easily be done by hand. If anything, this sounds like something to put into a django-package.

1 Like

Yes. I think I have to agree with you.

1 Like