Django: set session via Ajax

pistacchio

I'm trying to make an Ajax login form. I can get the user authenticated with django.contrib.auth.authenticate by passing it the username and password coming from the ajax post. But how can I them set an actual session and csrf so that in subsequent calls I can get the user in request.user?

Thanks

Glyn Jackson

Add {% csrf_token %} as normal then add a pre-request callback function beforeSend on your AJAX POST etc:

  beforeSend: function (request) {
   request.setRequestHeader("X-CSRFToken",
       $('input[name="csrfmiddlewaretoken"]').val());
   },

If using an API Framework like TastyPie you have other options for authentication. See here: How can I login to django using tastypie

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accesing and removing session that was set with Ajax

From Dev

Set the session of subdomain using ajax

From Dev

Set session value in Template Django

From Dev

flask: get session time remaining via AJAX

From Dev

How to update session array via ajax?

From Dev

Set Magento 2 session variable over AJAX

From Dev

jQuery AJAX - Set PHP Session variable

From Dev

jQuery and ajax to set session variable in PHP

From Dev

Set session variable with ajax call and refresh partial

From Dev

set cookie in php and run the session through ajax

From Dev

Set headers for AJAX request to Django

From Dev

django set session variable till end of day

From Dev

Set Value from JSON via AJAX

From Dev

Set delay between some echo via AJAX

From Dev

Is it possible to transmit PHP session id via ajax and store it with jquery cookie?

From Dev

Django Rest Framework: Upload file via AJAX

From Dev

Django download and display an image via ajax

From Dev

Set php session from ajax in iframe fetcher to be identified

From Dev

$_SESSION with Ajax

From Dev

Ajax and session

From Dev

Ajax and session

From Dev

Django - Session refreshes every time there's an ajax request

From Dev

In django-allauth how and where to set session variables after login?

From Dev

How can I set session never expire in django

From Dev

Session Cookie HTTPOnly flag not set on response from logout (Django)

From Dev

How to create a middleware that set a session variable on login in Django?

From Dev

I set a key in django's request.session , but has no effect

From Dev

jQuery array set both key and value and send array via AJAX

From Dev

React: initial render fires before state is set (via ajax)

Related Related

HotTag

Archive