How can I execute an AJAX request with an authenticated user in Django?

I need to make an HTTP call using AJAX with a validated user using Django.

Is there a standard easy way to do this? I read this other question , but uses a different technology.

I use HTTP (not HTTPS) and use a third-party application like django-tastypie for this unique β€œsimple” purpose would be redundant, right?

+4
source share
2 answers

Ajax requests are no different from any other request. If the user has already authenticated, then the Ajax request will also be checked: the session cookie that identifies the user will be sent along with the Ajax request in the same way as with any other.

(This related question does not matter, as it talks about authenticating the user in the request, and not about using the already authenticated user as you want.)

+5
source

To do this, you simply make an AJAX call to your URL, and in the view that processes the URL, you must add the @login_required decorator. For some reason, the django project is working right now, so I will send you to another place where you can get information .

However, you will need to keep track of some things, but you need to add the CSRF token, and secondly, you need to make a few changes to the SETTINGS.PY file.

I think the video covers all of this, I'm sorry that I cannot send you to official documents, because it does not seem to be working right now.

0
source

Source: https://habr.com/ru/post/1498689/


All Articles