lakshmen
lakshmen

Reputation: 29094

Customising the Django Admin change password page

I would like to have my own custom change_password page and I am already using the admin login from Django(using from django.contrib.auth.decorators import login_required).
Got the admin login working but would like to change the change_password page.

How do I do that?
I am not sure how to link to the admin login, or because I want to customize my change_password, I must customize my admin login as well?

Need some guidance. Thanks...

Upvotes: 5

Views: 6300

Answers (2)

super9
super9

Reputation: 30131

You can import the forms

from django.contrib.auth.views import password_change

If you look at Django's password_change view. You will notice that it takes it a view parameters which you can supply to customise the view to your own needs thus making your webapp more DRY.

def password_change(request,
                    template_name='registration/password_change_form.html',
                    post_change_redirect=None,
                    password_change_form=PasswordChangeForm,
                    current_app=None, extra_context=None):
    if post_change_redirect is None:
        post_change_redirect = reverse('django.contrib.auth.views.password_change_done')
    if request.method == "POST":
        form = password_change_form(user=request.user, data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(post_change_redirect)
    else:
        form = password_change_form(user=request.user)
    context = {
        'form': form,
    }
    if extra_context is not None:
        context.update(extra_context)
    return TemplateResponse(request, template_name, context,
                            current_app=current_app)

Most notably, template_name and extra_context such that your view looks like this

from django.contrib.auth.views import password_change

def my_password_change(request)
        return password_change(template_name='my_template.html', extra_context={'my_var1': my_var1})

Upvotes: 9

Hedde van der Heide
Hedde van der Heide

Reputation: 22459

Django's template finders let you override any template, in your template folder just add the admin templates you want to override, for example:

templates/
   admin/
      registration/
         password_change_form.html
         password_reset_complete.html
         password_reset_confirm.html
         password_reset_done.html
         password_reset_email.html
         password_reset_form.html

Upvotes: 4

Related Questions