Cookies not showing up in environ

abc abc useracct222 at gmail.com
Sat Jul 21 17:17:37 EDT 2018


> 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. Does your example represent one or the other?

My mistake, I should have mentioned I was using Django 1.11 so I had to use url instead of path in urls.py. Below, I have tried to provide as much information as possible.

I tried with just the files you provided with slight modifications:

project_folder/app/views.py:

    from django.http import HttpResponse

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

    def index(request):
        print 'INDEX: '
        print request.COOKIE
        return HttpResponse('this is the response')


project_folder/app/urls.py:

    from django.conf.urls import url
    from . import views

    urlpatterns = [
        url('cookies', views.cookies),
        url('index', views.index),
    ]


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')]


>From the project folder, I ran 'python manage.py runserver'

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.

I tried refreshing the browser for localhost:8000/index but still no cookies. Also, for views.py, the print statement 'INDEX: ' did not appear in the console, nor did the http response string I returned (in either browser or console). So something is not getting processed right. In my previous attempts (to your example code) I had no issue rendering templates and html. The only thing that didn't seem to work were the cookies.


Console Output:

Performing system checks...

System check identified no issues (0 silenced).
July 21, 2018 - 20:47:24
Django version 1.11.13, using settings 'app.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[21/Jul/2018 20:47:34] "GET / HTTP/1.1" 200 0
[21/Jul/2018 20:47:34] "GET /favicon.ico HTTP/1.1" 200 0
[21/Jul/2018 20:47:54] "GET / HTTP/1.1" 200 0
[21/Jul/2018 20:47:58] "GET /cookies HTTP/1.1" 200 0
[21/Jul/2018 20:47:58] "GET /favicon.ico HTTP/1.1" 200 0
[21/Jul/2018 20:48:07] "GET /index HTTP/1.1" 200 0
[21/Jul/2018 20:48:08] "GET /favicon.ico HTTP/1.1" 200 0



More information about the Python-list mailing list