如何使用自己的模板使用内置的密码重置/更改视图 [英] How do I use the built in password reset/change views with my own templates

查看:90
本文介绍了如何使用自己的模板使用内置的密码重置/更改视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我可以将 url'^ / accounts / password / reset / $'指向 django.contrib.auth.views.password_reset 在上下文中使用我的模板文件名,但我认为需要发送更多的上下文详细信息。



我需要知道每个的密码重置和更改视图。

解决方案

如果您查看 django.contrib.auth.views.password_reset ,你会看到它使用< a href =http://code.djangoproject.com/browser/django/trunk/django/template/__init__.py =noreferrer> RequestContext 。结果是,您可以使用上下文处理器来修改可能允许您注入所需信息的上下文。



b列表具有良好的上下文处理器的介绍。 p>

编辑(我似乎对实际问题感到困惑):



你会注意到 password_reset 采用一个名为 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):

检查< a href =https://docs.djangopro ject.com/en/1.9/topics/auth/default/#django.contrib.auth.views.password_resetrel =noreferrer> password_reset 获取更多信息。



...因此,使用urls.py,如下所示:

  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 ='my_templates / password_reset.html'



否则,你不需要提供任何上下文,因为 password_reset 视图照顾自己。如果要查看可用的上下文,可以触发 TemplateSyntax 错误,并查看堆栈跟踪,找到一个名为的局部变量的框架上下文。如果你想修改上下文,那么上面关于上下文处理器的内容可能就是要走了。



总而言之,你需要做些什么来使用自己的模板?在调用视图时,提供 template_name 关键字参数。您可以通过将字典包含为URL模式元组的第三个成员来为视图提供关键字参数。


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.

解决方案

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.

The b-list has a good introduction to context processors.

Edit (I seem to have been confused about what the actual question was):

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):

Check password_reset for more information.

... 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 will be called for URLs matching '/accounts/password/reset' with the keyword argument template_name = 'my_templates/password_reset.html'.

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.

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.

这篇关于如何使用自己的模板使用内置的密码重置/更改视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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