Forums

Django request/session variables not working

Hey there,

I'm trying to call session variables in templates.

On my local machine I would write: {{ request.session.keys }} --> all session keys {{ request.user }} --> Jax472

On the app here: {{ request.session.keys }} --> nothing {{ request.user }} --> nothing {{ user }} --> Jax472 {{ session.keys }} --> nothing

sessions are installed in middleware, and in installed apps for database driven sessions. And I am confused as to why {{ user }} returns a value but {{ request.user }} (and any requests for that matter) do not.

Appreciate any help, all the code works on the local machine, just need to figure out what's going on here.

You're probably setting the session cookie on the wrong domain. Have a look at your settings to ensure that SESSION_COOKIE_DOMAIN is set correctly.

I haven't changed SESSION_COOKIE_DOMAIN. It should be set to none. Per your advice, I manually changed it in settings.py , relaunched the app, and am still getting the same problem. It's also very confusing as I can't access the request objects using {{ request.something }}.

I should note that I have not tried to deploy the code from my local machine to here. I simply launched a new web app via the user console and changed the DB to fetch the mysql DB, and have been coding via the bash console. Therefore the project should be using all default values.

Heres the WSGI:

"""
WSGI config for project project.
It exposes the WSGI callable as a module-level variable name ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
"""
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

and settings.py

"""
Django settings for project project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = REMOVED
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'urbis',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    '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',
)

ROOT_URLCONF = 'project.urls'

WSGI_APPLICATION = 'project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
   REMOVED
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

#Cookie Domain
SESSION_COOKIE_DOMAIN = ".jax472.pythonanywhere.com"

You're right about SESSION_COOKIE_DOMAIN, I misread the docs. You should use None, there.

The next things I would think about is: Are you injecting the appropriate values into the template context so the template can access them? Do you have access to the request.session in the view and are they just missing from the template?

"Are you injecting the appropriate values into the template context so the template can access them?"

That was the problem right there. For some reason on my local machine I don't have to pass request into the context.

For example:

def my_view(request): return render(request, "someview.html", {})

allows me to access the request object as expected (since it's passed in as the first parameter)

However for the project here it wont, it needs to be passed in explicitly in the context:

def my_view(request): return render(request, "someview.html", {'request':request})

Odd. I'm guessing it has something to do with the Django version I'm using on my machine (1.9.2) and the version I'm using here(1.3.7)

https://help.pythonanywhere.com/pages/VirtualEnvForNewerDjango/

I'm assuming I can just go through that documentation to setup the virtualenv, install the most current version, and everything should work as I expect it to.

Thanks!

Yup. That will do it.

no its still not working.

What's not working? What are you doing and what is happening?