【问题标题】:Customising the Django Admin change password page自定义 Django 管理员更改密码页面
【发布时间】:2012-06-29 07:14:48
【问题描述】:

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

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

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

【问题讨论】:

    标签: django django-admin


    【解决方案1】:

    您可以导入表格

    from django.contrib.auth.views import password_change

    如果您查看 Django 的 password_change 视图。您会注意到它需要一个视图参数,您可以提供这些参数来根据自己的需要自定义视图,从而使您的 web 应用程序更加干燥。

    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)
    

    最值得注意的是,template_nameextra_context 使您的视图看起来像这样

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

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

      • 意思是说我的模板文件夹中应该有一个额外的管理文件夹?我猜对了吗?或编辑 django 提供的管理文件夹?顺便说一句,django 管理文件夹在哪里?
      • 您似乎没有。我在我的应用程序中添加了管理模板,但 django 仍然选择默认的管理模板。
      • @Pasada 可能是因为您的应用位于INSTALLED_APPS 中的django.contribute.admin 下。
      • @Gregory Goltsov 谢谢,你最后的评论帮助了我。 :)
      最近更新 更多