Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 296
lakshmen Avatar asked Jun 29 '12 07:06

lakshmen


1 Answers

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})
like image 174
super9 Avatar answered Nov 11 '22 13:11

super9