如何在我自己的模板中使用内置的密码重置/更改视图
问题描述
例如,我可以使用我的模板文件名将 url '^/accounts/password/reset/$'
指向 django.contrib.auth.views.password_reset
上下文,但我认为需要发送更多上下文详细信息.
For example I can point the url '^/accounts/password/reset/$'
to django.contrib.auth.views.password_reset
with my template filename in the context but I think need to send more context details.
我需要确切知道要为每个密码重置和更改视图添加什么上下文.
I need to know exactly what context to add for each of the password reset and change views.
解决方案
如果你看一下 django.contrib.auth.views.password_reset 你会看到它使用 RequestContext
.结果是,您可以使用上下文处理器来修改上下文,这可能允许您注入您需要的信息.
If you take a look at the sources for django.contrib.auth.views.password_reset you'll see that it uses RequestContext
. The upshot is, you can use Context Processors to modify the context which may allow you to inject the information that you need.
b-list 有很好的 上下文处理器简介.
The b-list has a good introduction to context processors.
编辑(我似乎对实际问题感到困惑):
Edit (I seem to have been confused about what the actual question was):
您会注意到 password_reset
采用名为 template_name
的命名参数:
You'll notice that password_reset
takes a named parameter called template_name
:
def password_reset(request, is_admin_site=False,
template_name='registration/password_reset_form.html',
email_template_name='registration/password_reset_email.html',
password_reset_form=PasswordResetForm,
token_generator=default_token_generator,
post_reset_redirect=None):
检查 password_reset 了解更多信息.
Check password_reset for more information.
... 因此,使用 urls.py 如下:
... thus, with a urls.py like:
from django.conf.urls.defaults import *
from django.contrib.auth.views import password_reset
urlpatterns = patterns('',
(r'^/accounts/password/reset/$', password_reset, {'template_name': 'my_templates/password_reset.html'}),
...
)
django.contrib.auth.views.password_reset
将调用匹配 '/accounts/password/reset'
与关键字参数 template_name = ' 的 URLmy_templates/password_reset.html'
.
django.contrib.auth.views.password_reset
will be called for URLs matching '/accounts/password/reset'
with the keyword argument template_name = 'my_templates/password_reset.html'
.
否则,您不需要提供任何上下文,因为 password_reset
视图会自行处理.如果您想查看可用的上下文,可以触发 TemplateSyntax
错误并查看堆栈跟踪,找到带有名为 context
的局部变量的框架.如果你想修改上下文,那么我上面所说的上下文处理器可能是要走的路.
Otherwise, you don't need to provide any context as the password_reset
view takes care of itself. If you want to see what context you have available, you can trigger a TemplateSyntax
error and look through the stack trace find the frame with a local variable named context
. If you want to modify the context then what I said above about context processors is probably the way to go.
总而言之:您需要做什么才能使用自己的模板?在调用视图时为视图提供 template_name
关键字参数.您可以通过将字典作为 URL 模式元组的第三个成员来为视图提供关键字参数.
In summary: what do you need to do to use your own template? Provide a template_name
keyword argument to the view when it is called. You can supply keyword arguments to views by including a dictionary as the third member of a URL pattern tuple.
相关文章