5

我想拥有自己的自定义change_password页面,并且我已经在使用 Django 的管理员登录名(使用from django.contrib.auth.decorators import login_required)。
让管理员登录工作,但想更改change_password页面。

我怎么做?
我不确定如何链接到管理员登录,或者因为我想自定义我的 change_password,我也必须自定义我的管理员登录?

需要一些指导。谢谢...

4

2 回答 2

9

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})
于 2012-06-29T12:41:49.857 回答
4

Django 的模板查找器允许您覆盖任何模板,在您的模板文件夹中只需添加您想要覆盖的管理模板,例如:

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
于 2012-06-29T09:30:08.260 回答