无法调用自定义上下文处理器
我正在尝试创建一个自定义上下文处理器,它将为登录用户呈现菜单项列表。我已经完成了以下操作:
在我的settings.py中,我
TEMPLATE_CONTEXT_PROCESSOR = ( 'django.contrib.auth.context_processors.auth', 'mysite.accounts.context_processors.user_menu', )
在accounts子模块下有context_processors.py
,目前包含以下内容:
def user_menu(request): return {'user_menu':'Hello World'}
在我的模板页面上,我有以下:
{% if user.is_authenticated %} Menu {{user_menu}} {% endif %}
调用视图如下:
def profile(request): return render_to_response('accounts/profile.html',context_instance=RequestContext(request))
但是我无法获取 {{user_menu}}
来呈现页面上的任何内容,我知道用户已通过类似检查的模板的其他部分进行身份验证正确渲染。我在这里错过了什么吗?请帮忙 谢谢
编辑: 谢谢 Ben、Daniel,我已经修复了 TEMPLATE_CONTEXT_PROCESSOR
中的 (S),但是 Django 现在无法解析该模块,我收到以下消息
Error importing request processor module django.contrib.auth.context_processors: "No module named context_processors"
更新:我通过更改django.core.context_processors.auth
的路径来修复它似乎模块已被移动
I am trying to create a custom context processor which will render a list of menu items for a logged in user. I have done the following:
Within my settings.py I have
TEMPLATE_CONTEXT_PROCESSOR = ( 'django.contrib.auth.context_processors.auth', 'mysite.accounts.context_processors.user_menu', )
Under the accounts submodule I have context_processors.py
with the following, for now:
def user_menu(request): return {'user_menu':'Hello World'}
On my template page I have the following:
{% if user.is_authenticated %} Menu {{user_menu}} {% endif %}
The invoking view is as follows:
def profile(request): return render_to_response('accounts/profile.html',context_instance=RequestContext(request))
However I am unable to get the {{user_menu}}
to render anything on the page, I know the user is authenticated as other sections of the template with similar checks render correctly. Am I missing something here. Please help
Thank you
Edit: Thanks Ben, Daniel, I have fixed the (S) in TEMPLATE_CONTEXT_PROCESSOR
, however Django now has trouble resolving the module and I get the following message
Error importing request processor module django.contrib.auth.context_processors: "No module named context_processors"
UPDATE: I fixed it by changing the path to django.core.context_processors.auth
Seems like the modules have been moved around
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置名称应为
TEMPLATE_CONTEXT_PROCESSORS
,带有 S。The setting name should be
TEMPLATE_CONTEXT_PROCESSORS
, with an S.