Saturday, July 21, 2018

Re: Django + WSGI: no HTTP_COOKIE in environ

On donderdag 19 juli 2018 10:34:35 CEST need_some_help wrote:

 

> I first need to make sure I am accessing them correctly. This is as simple

> as it gets I imagine:

>

> view.py:

>

> def index(environ, start_response, request):

> if not 'HTTP_COOKIE' in environ:

> response = HttpResponse("hello")

> response.set_cookie('user_agreement', 'cookie text',

> domain='.mysite.com')

> return response

> else:

> print environ['HTTP_COOKIE']

>

> The webpage just prints 'hello' and never reaches the else not matter how

> many times I refresh the page. There are no cookies in the browser ever

> either.

>

> Am I missing some middleware?

 

No, you have the wrong assumption about a view method's signature. The first argument is always the request, as in django.http.HttpRequest or more specifically, it's subclass django.core.handlers.WSGIRequest in the case of WSGI.

You're not writing a WSGI handler (who has environ as first argument). A view is two stops down and to give you the complete onion:

WSGI Handler -> Middleware -> view -> Middleware -> WSGI Handler

 

Anyway, the cookies are at request.COOKIES as per docs.

--

Melvyn Sopacua

No comments:

Post a Comment