I'm trying to change some feature on Askbot to make it show different versions of the user-card depending on the session. I learned that I can pass request.session just like any other data from view to template. However, the template that I want to change is buried inside of many parent templates that are eventually rendered by a view.
I have included the context processor(I'm using Django 1.8):
TEMPLATES = (
{
'BACKEND': 'askbot.skins.template_backends.AskbotSkinTemplates',
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
]
}
},
)
in my view:
def question(request, id, no_rep):
if id == 1:
request.session['no_rep'] = True
else:
request.session['no_rep'] = False
return render(request, 'question.html', data)
In my template:
{% if request.session.no_rep == True %}
I'm still getting the error 'request' is undefined
.
Am I missing something obvious?
This is the full traceback:
Traceback:
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/views/readers.py" in question
687. return render(request, 'question.html', data)
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-venv/local/lib/python2.7/site-packages/django/shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-venv/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
99. return template.render(context, request)
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/skins/template_backends.py" in render
86. return self.template.render(context)
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-venv/local/lib/python2.7/site-packages/coffin/template/__init__.py" in render
55. return super(Template, self).render(**context)
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-venv/local/lib/python2.7/site-packages/jinja2/environment.py" in render
989. return self.environment.handle_exception(exc_info, True)
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-venv/local/lib/python2.7/site-packages/jinja2/environment.py" in handle_exception
754. reraise(exc_type, exc_value, tb)
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/question.html" in top-level template code
388. {% from "macros.html" import comment_widget, tag_widget %}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/two_column_body.html" in top-level template code
1. {% extends "base.html" %}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/base.html" in top-level template code
45. {% block body %}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/two_column_body.html" in block "body"
5. {% block content%}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/question.html" in block "content"
382. {% include "question/content.html" %}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/question/content.html" in top-level template code
3. {% include "question/question_card.html" %}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/question/question_card.html" in top-level template code
31. {% include "question/question_author_info.html" %}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/question/question_author_info.html" in top-level template code
1. {{ macros.post_last_updater_and_creator_info(question) }}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/macros.html" in template
108. {{ post_contributor_info(first_rev, "original_author") }}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/macros.html" in template
216. {{ post_contributor_avatar_and_credentials(revision) }}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/macros.html" in template
64. {{ user_card(revision.author) }}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/macros.html" in template
649. {% include "widgets/user_card.html" %}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-devel/askbot/templates/widgets/user_card.html" in top-level template code
13. {% if request.session.no_rep == True %}
File "/mnt/c/Users/Lea Quan/Desktop/LITWC/askbot-venv/local/lib/python2.7/site-packages/jinja2/environment.py" in getattr
408. return getattr(obj, attribute)
Exception Type: UndefinedError at /question/1/how-to-deselect-word-in-thinking-vs-clicking-in-modern-user-interfaces/
Exception Value: 'request' is undefined
request
context variable (i.e. havedjango.core.context_processors.request
in yoursettings.py
) you can simply access to any session variable using{{ request.session.my_var }}
. See this question for a detailed explanation. – Selcukreturn render(request, 'question.html', data)
in my view. Isrequest.session
only accessible inquestion,html
? I'm trying to accessrequest.session
from a template that's included very deep down fromquestion.html
. And currently I'm getting an exception saying'request' is undefined
. – Lea Quanrequest
in yourdata
dictionary? If you do that, it will only be available to that view. That's why you need to includedjango.core.context_processors.request
in yourCONTEXT_PROCESSORS
setting. Context processors inject extra context variables to all views so that you Don't have to Repeat Yourself. – Selcukdjango.core.context_processors.request
and leavedjango.template.context_processors.request
only. Please post the error message with full traceback. – Selcuk