to django.c...">

How to use the built-in password reset / change views using custom templates

For example, I can point url '^/accounts/password/reset/$' to django.contrib.auth.views.password_reset with my template file name in context, but it seems to me that you need to send more context details.

I need to know exactly what context to add for each of the passwords reset and change the views.

+83
python django passwords
Dec 23 '08 at 12:47
source share
7 answers

If you look at the sources of django.contrib.auth.views.password_reset , you will see that it uses RequestContext . As a result, you can use Context Processors to change the context, which can allow you to enter the information you need.

The b-list has a good view for context processors .

Edit (I seem to have been confused by what was actually):

You will notice that password_reset accepts a named parameter named 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): 

Learn more about password_reset .

... this way with 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 do not need to provide any context since the password_reset view takes care of itself. If you want to know what context you have, you can throw a TemplateSyntax error and look at the stack trace to find a frame with a local variable called context . If you want to change the context, then what I said above about context handlers is probably the way to go.

In short: what do you need to do to use your own template? Provide the keyword argument template_name to represent when invoked. You can put keyword arguments in views by including the dictionary as the third term in the tuple of URL patterns.

+92
Dec 23 '08 at 13:21
source share

Highly recommend this article.

I just plugged it in and worked

http://garmoncheg.blogspot.com.au/2012/07/django-resetting-passwords-with.html

+25
Feb 14 '13 at 5:52
source share

You just need to wrap existing functions and pass in the template you need. For example:

 from django.contrib.auth.views import password_reset def my_password_reset(request, template_name='path/to/my/template'): return password_reset(request, template_name) 

To see this, just take a look at the declartion function of the inline views:

http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/views.py#L74

+10
Dec 23 '08 at 18:27
source share

You can do the following:

  • add to your urls (r '^ / accounts / password / reset / $', password_reset)
  • put your template in '/templates/registration/password_reset_form.html'
  • make your application before "django.contrib.auth" in INSTALLED_APPS

Explanation:

When templates are loaded, they are executed in your INSTALLED_APPS variable in settings.py. The order is dictated by the rank of the definition in INSTALLED_APPS, since your application appeared before "django.contrib.auth", your template was downloaded (link: https://docs.djangoproject.com/en/dev/ref/templates/api/#django .template.loaders.app_directories.Loader ).

Motivation Approach:

  • I want to be drier and not be repeated for any representation (defined by django) of the template name (they are already defined in django)
  • I need the smallest url.py
+6
Apr 24 '13 at 13:20
source share

the documentation says that there is only one context variable form .

If you have problems with the login (which is common), the documentation says that there are three context variables:

  • form : a form object representing the login form. For more information about forms, see the documentation for forms.
  • next : URL to redirect after successful login. It may also contain a query string.
  • site_name : the name of the current site, as configured by SITE_ID.
+1
Dec 23 '08 at 12:54
source share

I used these two lines in the URL and the admin template, which I changed in my need

 url(r'^change-password/$', 'django.contrib.auth.views.password_change', { 'template_name': 'password_change_form.html'}, name="password-change"), url(r'^change-password-done/$', 'django.contrib.auth.views.password_change_done', { 'template_name': 'password_change_done.html' }, name="password-change-done") 
+1
Mar 04 '13 at 14:11
source share

Another, possibly simpler, solution is to add your override pattern directory to the TIRPLATES parameter's DIRS entry in settings.py. (I think this option is new in Django 1.8. It could be called TEMPLATE_DIRS in previous versions of Django.)

Same:

 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', # allow overriding templates from other installed apps 'DIRS': ['my_app/templates'], 'APP_DIRS': True, }] 

Then put your override template files in my_app/templates . So the reset password override template will be my_app/templates/registration/password_reset_form.html

0
Nov 26 '16 at 22:35
source share



All Articles