Forums

AttributeError at / 'WSGIRequest' object has no attribute 'session'

Edit: [SOLVED] Was using different django version on local host.

I have no idea what to do, my search only yielded this forum topic.

I don't know what to make of SESSION_COOKIE_DOMAIN? so I added to the end of my settings.py:

#Cookie Domain
SESSION_COOKIE_DOMAIN = 'none'

But I still see the error:

Django Version: 1.9.3

Exception Type: AttributeError

Exception Value: 'WSGIRequest' object has no attribute 'session'

edit: I should add that everything worked great when used locally.

What's the traceback?

Environment:

Request Method: POST Request URL: http://www.mathmaker.xyz/

Django Version: 1.9.3 Python Version: 3.5.2 Installed Applications: ['generator',  'django.contrib.admin',  'django.contrib.auth',  'django.contrib.contenttypes',  'django.contrib.sessions',  'django.contrib.messages',  'django.contrib.staticfiles'] Installed Middleware: ['django.middleware.common.CommonMiddleware',  'django.middleware.csrf.CsrfViewMiddleware']

Traceback:

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/McKlatch/mathmaker/generator/views.py" in index
  43.     request.session["questions"] = printed_qna['questions']

Exception Type: AttributeError at / Exception Value: 'WSGIRequest' object has no attribute 'session'

I suppose it could be installed middleware... or lack thereof?

Do you have the django sessionmiddleware activated?

.

MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]

I never deactivated it as far as I know.

Is there something i need to do other than make sure that

'django.contrib.sessions.middleware.SessionMiddleware',

is present?

so the middleware ordering also matters but most of it seems fine. The weirdest one is the clickjacking one. try taking that out and reloading and seeing what happens?

same error.

The whole project worked locally, without any django debugging display...

so I'm wondering if there is a specific instruction I need to make the 'session' variable work on pythonanywhere

Hmm I am definitely not aware of anything that strips out sessions. Will investigate a bit. Is this happening just for specific views? (ie. is session availble sometimes?)

Were you using python3.5 with a virtualenv locally?

I have to assume that the problem is global. The session data is only used for a presentation view and a view which serves a text file (for user to download), the latter is only reachable by the user after the former.

from views.py:

def index(request):
    context = {}

    #### sends form to new visitors
    if request.method == 'GET':

        # the numbers from which multiples can be chosen
        context["quantity"] = quantity
        context["operators"] = opChoices
        context["limits"] = limits
        context["multChoices"] = multChoices
        return render(request, 'generator/form.html', context)

    ### reading submitted form data for parameters
    params = {
        'quantity': request.POST['quantity'],
        'operators': getOps(request.POST),
        'attributes': getAttr(request.POST),
        'number0': numberSymbol(int(request.POST['leftRange-lower']),
                                int(request.POST['leftRange-upper']),
                               getMults(request.POST)[0]),
        'number1': numberSymbol(int(request.POST['rightRange-lower']),
                                int(request.POST['rightRange-upper']),
                               getMults(request.POST)[1])
    }

    ### generate questions based on collected information
    generated_qna = generateQuestions(params)

    # line by line data
    printed_qna = generatePresentation(generated_qna)
    context["params"] = params
    request.session["questions"] = printed_qna['questions']
    request.session["answers"] = printed_qna['answers']
    context["questions"] = request.session["questions"]
    context["answers"] = request.session["answers"]

    # clean operator symbols for human reading
    context["humanOperators"] = (human_list(human_clean(params["operators"])))

    return render(request, 'generator/generated.html', context)


def download(request):
    context = {}

    #### prevents accessing the wrong page
    if request.method == 'GET':

        # force 404
        return HttpResponse("such hack, very disappoint, wow")

    ### download text files
    content = (docText["title"] + "\n"
               + docText["slogan"] +"\n\nQuestions only\n"
               + ''.join(request.session["questions"])
               + "\n"
               + "Questions with Answers\n"
               + ''.join(request.session["answers"]))
    response = HttpResponse(content_type='text/plain; charset=utf8')
    response['Content-Disposition'] = 'attachment; filename="%s.txt"' % "MathMaker"
    response.write(content)
    return response

locally I was using python 3.5 no virtualenv.

Ohhhh. It's because you were using django 1.10 locally and 1.9 on pythonanywhere. One is MIDDLEWARE, the other is MIDDLEWARE_CLASSES. I would setup a virtualenv both locally and on pythonanywhere to make sure you have the correct versions for different packages.

Thank you, changing the one variable name fixed it.

Thank you so much conrad.

I will work on my virtualenv game from now on.