1👍
✅
just catch csrf_token in your html page in script tag at a time of document ready by
var csrf = {{ csrf_token }}
and then via ajax pass parameter like in your js file
$.ajax({
csrfmiddlewaretoken: csrf; ..//
});
3👍
Extending @Zigs Answer .
You can generate csrf token at server(Django) side using :
from django.middleware.csrf import _get_new_csrf_key as get_new_csrf_key
response.set_cookie("csrftoken", get_new_csrf_key())
Use this csrf token for subsequent POST (ajax) request.
- [Django]-Django – Custom Template Tag passing keyword args
- [Django]-RegexValidator is not allowing 0-9 in CharField
- [Django]-How can I call model methods or properties from Django Admin?
- [Django]-Why does django parse tags in commented HTML code?
- [Django]-Overriding Django Admin's main page? – Django
Source:stackexchange.com