我正在为一家小公司工作,该公司的用户应该能够发布。我想象了一个非常简单的身份验证机制,人们只需输入他们的电子邮件地址,并发送一个唯一的登录网址,设置一个cookie,以备将来的要求。
在我的模板设置中,我有base.html,其他页面扩展了这个。我想在base.html中显示登录或注册按钮,但是如何确保必要的变量始终是上下文的一部分?看来,每个视图只是设置它们喜欢的上下文,并且没有全局上下文的人口。有没有办法这样做,而不必在每个上下文创建中包含用户?
或者,我必须自己创建自定义快捷方式来正确设置上下文吗? >
解决方案在更一般的意义上,不必在每个视图中显式设置变量,这听起来就像您想要编写自己的上下文处理器。 p>
从文档中:
上下文处理器有一个非常简单的界面:只是一个Python函数,它接受一个参数HttpRequest对象,并返回一个添加到模板上下文中的字典。每个上下文处理器必须返回一个字典。
I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.
In my template setup, I have base.html, and the other pages extend this. I want to show logged in or register button in the base.html, but how can I ensure that the necessary variables are always a part of the context? It seems that each view just sets up the context as they like, and there is no global context population. Is there a way of doing this without including the user in each context creation?
Or will I have to make my own custom shortcuts to setup the context properly?
解决方案In a more general sense of not having to explicitly set variables in each view, it sounds like you want to look at writing your own context processor.
From the docs:
A context processor has a very simple interface: It's just a Python function that takes one argument, an HttpRequest object, and returns a dictionary that gets added to the template context. Each context processor must return a dictionary.
更多推荐
始终将用户包含在django模板上下文中
发布评论