Cookies not showing up in environ

Jon Ribbens jon+usenet at unequivocal.eu
Sat Jul 21 20:48:07 EDT 2018


On 2018-07-21, abc abc <useracct222 at gmail.com> wrote:
>> I think one of the main issues is that you don't seem to have decided
>> whether you're writing a WSGI application or a Django application.
>
> Yes, I suppose I thought Django had to use wsgi to process requests,
> I didn't know there were 'two' options here.

Django does use wsgi to process requests. But if you've written
an application in Django, you don't need to then write it again
without Django - Django has its own wsgi app that will call the
Django app for you.

> Does your example represent one or the other?

Both - I gave two alternatives, and labelled each. You only need one.

> project_folder/app/wsgi.py
>
>     def application(environ, start_response):
>         start_response('200 OK',
>                        [('Content-Type', 'text/plain; charset=utf-8')])
>         return [environ.get('HTTP_COOKIE', '').encode('utf-8')]

You should delete that and change it back to whatever 'django
startproject' gave you originally for wsgi.py. If you're writing
a django app then you don't need to also write a wsgi app.

> Cleared all cookies in the browser, and went to
> localhost:8000/cookies which loaded with no error in the console or
> browser. No cookies were created in the browser preferences.

Well, you're not setting any cookies. You'd need to do something like:

     def cookies(request):
         response = HttpResponse(repr(request.COOKIES),
                                 'text/plain; charset=utf-8')
         response.set_cookie('mycookie', 'value')
         return response



More information about the Python-list mailing list