1

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
Lea Quan
  • 23
  • 8
  • There is no difference between a base template vs a child template in terms of using context variables. If you have access to `request` context variable (i.e. have `django.core.context_processors.request` in your `settings.py`) you can simply access to any session variable using `{{ request.session.my_var }}`. See [this question](https://stackoverflow.com/questions/2551933/django-accessing-session-variables-from-within-a-template) for a detailed explanation. – Selcuk Jul 24 '18 at 23:38
  • I guess I'm confused about the scope of these context. if i have `return render(request, 'question.html', data)` in my view. Is `request.session` only accessible in `question,html`? I'm trying to access `request.session` from a template that's included very deep down from `question.html`. And currently I'm getting an exception saying `'request' is undefined`. – Lea Quan Jul 25 '18 at 00:12
  • Are you manually including `request` in your `data` dictionary? If you do that, it will only be available to that view. That's why you need to include `django.core.context_processors.request` in your `CONTEXT_PROCESSORS` setting. Context processors inject extra context variables to all views so that you Don't have to Repeat Yourself. – Selcuk Jul 25 '18 at 00:24
  • Hi, I'm not including request manually, and I think I have the processors set up correctly. However, I'm still getting the error and I've included my code above. – Lea Quan Jul 25 '18 at 19:35
  • Remove `django.core.context_processors.request` and leave `django.template.context_processors.request` only. Please post the error message with full traceback. – Selcuk Jul 26 '18 at 00:06
  • I've included the error message and full traceback above! – Lea Quan Jul 26 '18 at 04:22

1 Answers1

0

I figured out the problem. The view I'm trying to access the variable from is disconnected from the template in the way that it's called but not included. I had to pass the request variable along to that view.

Lea Quan
  • 23
  • 8