如何在Django中访问子模板中的会话变量?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Django中访问子模板中的会话变量?相关的知识,希望对你有一定的参考价值。
我正在尝试更改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
答案
我解决了这个问题。我正在尝试访问变量的视图以与调用它但未包含的方式从模板断开连接。我不得不将请求变量传递给该视图。
以上是关于如何在Django中访问子模板中的会话变量?的主要内容,如果未能解决你的问题,请参考以下文章