始终包括Django模板上下文中的用户 [英] Always including the user in the django template context

查看:114
本文介绍了始终包括Django模板上下文中的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个小的内部网站的一个小公司,在那里用户应该能够发布。我想到了一个非常简单的身份验证机制,人们只需输入自己的电子邮件地址,并获取发送一个唯一的登录URL,即设置一个cookie,将永远找出他们将来的请求。

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.

在我的模板的设置,我base.html文件,而其他页面扩展此。我想显示登录或注册base​​.html文件按钮,但我怎么能保证必需的变量总是上下文的一部分?似乎每个视图只设置自己喜欢的背景下,并没有全球背景下的人口。是否有这样做,而不包括在每个上下文创建用户的一种方式?

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?

推荐答案

在没有明确设定变量在每个视图更一般意义上,这听起来像你想看看编写自己的<一个href=\"http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors\">context处理器。

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.

从文档:

一个上下文处理器有一个非常简单的接口:它只是一个Python函数,有一个参数,一个Htt的prequest对象,并返回被添加到模板上下文的字典。每个上下文处理器必须返回一个字典。

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模板上下文中的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆