如何在Django的子模板中访问会话变量?

时间:2018-07-24 22:47:32

标签: html django templates session view

我正在尝试更改Askbot的某些功能,以使其根据会话显示不同版本的用户卡。我了解到,我可以像从视图到模板的任何其他数据一样传递request.session。但是,我要更改的模板埋在许多最终由视图呈现的父模板中。

我包括了上下文处理器(我正在使用 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',
            ]
        }
    },
)

在我看来:

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)

在我的模板中:

{% if request.session.no_rep == True %}

我仍然收到错误'request' is undefined。 我缺少明显的东西吗?

这是完整的追溯:


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

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。我尝试从中访问变量的视图以调用但未包含的方式与模板断开连接。我必须将请求变量传递给该视图。