How to use session in django

23 views
Skip to first unread message

Amit Agarwal

unread,
Nov 2, 2019, 10:11:12 PM11/2/19
to Django users
Helo everyone,

I am making a post request to the server from client.py. I am getting error forbidden csrf cookie not set.

What is the correct way to use session?
 

Integr@te System

unread,
Nov 3, 2019, 2:50:37 AM11/3/19
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/703a7b26-dbeb-43ba-83c8-66dab79afb1b%40googlegroups.com.

Parth Joshi

unread,
Nov 3, 2019, 8:58:43 AM11/3/19
to Django users
Hi Amit,

Here there are two ways:

  1. Capture the csrf cookies and send it with request as we do it for AJAX request. https://docs.djangoproject.com/en/2.2/ref/csrf/#ajax. Here you will have to figure it out for python.
  2. If you are doing it for testing purpose and want to bypass csrf (not recommended in testing and production environment), then either:
    1. Exempt your views by using @csrf_exempt (https://docs.djangoproject.com/en/2.2/ref/csrf/#edge-cases)
    2. Or remobe the csrf middleware(django.middleware.csrf.CsrfViewMiddleware) all together from settings.py (https://docs.djangoproject.com/en/2.2/ref/csrf/#how-to-use-it
Ideally you should not bypass csrf as its a good feature given by django even for development purpose. There will be a workover effort for putting it back in production. 

 Hope this is what you are looking for and it helps.

regards,

Parth
Reply all
Reply to author
Forward
0 new messages