如何覆盖Django管理面板以重置密码而不是使用我的模板?

14

我正在遵循这篇博客中有关在Django中重置用户密码的内容。它完美地运作。但问题是,我想在重置密码或确认邮件时显示我的模板,而不是Django管理面板。我该如何实现?

这是我的urls.py文件:

url(r'^password_reset/$', password_reset , name='password_reset_reset1'),
url(r'^password_reset/done/$', password_reset_done, name='password_reset_done'),
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
    password_reset_confirm, name='password_reset_confirm'),
url(r'^reset/done/$', password_reset_complete, name='password_reset_complete'),

我需要为模板和视图采取哪些步骤?我尝试了很多方法并添加了一些文件,例如:

registration/password_reset_form.html
registration/password_reset_subject.txt
registration/password_reset_email.html 
registration/password_reset_done.html
registration/password_reset_confirm.html 
registration/password_reset_complete.html

但是没有影响>我只想在重置密码时渲染我的网站模板。

这是我的目录结构:

├── backmyitem
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── feed
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_auto_20180804_1610.py
│   │   ├── 0003_auto_20180805_0533.py
│   │   ├── 0004_claimform.py
│   │   ├── 0005_auto_20180807_1403.py
│   │   ├── 0006_auto_20180807_1840.py
│   │   ├── 0007_auto_20180809_0045.py
│   │   ├── 0008_auto_20180809_0126.py
│   │   ├── 0009_auto_20180809_0140.py
│   │   ├── 0010_report_item_owner.py
│   │   ├── 0011_usernotification.py
│   │   ├── 0012_auto_20180813_0051.py
│   │   ├── 0013_auto_20180815_0159.py
│   │   ├── __init__.py
│   ├── models.py
│   ├── templates
│   │   ├── feed
│   │   │   ├── base.html
│   │   │   ├── claimform_form.html
│   │   │   ├── detail.html
│   │   │   ├── footer.html
│   │   │   ├── form_template.html
│   │   │   ├── header.html
│   │   │   ├── index.html
│   │   │   ├── loggedin.html
│   │   │   ├── login_user.html
│   │   │   ├── notification.html
│   │   │   ├── profile.html
│   │   │   ├── report_item_confirm_delete.html
│   │   │   ├── report_item_form.html
│   │   │   ├── SignUp.html
│   │   │   └── usernotification_form.html
│   │   ├── notification
│   │   └── registration
│   │       ├── form_login_template.html
│   │       └── login.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
└── myammaji

谢谢!


你有查看 Django 文档吗?我认为你在几个模板上走对了方向。只需要在 urls.py 中添加一些内容即可 :) https://docs.djangoproject.com/en/2.1/topics/auth/default/#using-the-views - The Pjot
我已经完成了。我前往管理根目录 /home/imsaiful/Desktop/local_repo/myvenv/lib/python3.6/site-packages/django/contrib/admin/templates 并更改基础扩展文件的名称。感谢您的帮助。 - imsaiful
很遗憾,这些都是过时的语法。 - Soren
10个回答

23
你还可以确保你的应用程序在INTALLED_APPS中排在所有其他Django应用程序之前,例如。
INSTALLED_APPS = [
'your_app_name',

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.staticfiles',
]

用你的应用程序的名称替换 your_app_name


1
这将导致管理员面板使用您的视图而不是管理员的视图。这不是一个好的解决方案,因为它会扭曲管理员面板用户体验。 - flaab
将你的应用程序移动到INSTALLED_APPS的顶部不会有所帮助。我已经检查过了。 - Dat TT

5
在您的settings.py中,请确保您的TEMPLATES设置等于以下内容。
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',

        ],
    },
},]

这里最重要的部分是DIRS

我已经尝试过了,但没有效果。我该如何在我的模板中使用password_reset点?模板目录应该放在哪里? - imsaiful
1
是的,'DIRS' 是正确的位置来进行更正。因此,在这种情况下,它应该是: 'DIRS': [os.path.join(BASE_DIR,'feed','templates')], - Dat TT

2
为了覆盖密码重置的管理模板,需要按照以下文件夹结构,并将自定义模板放置在 templates/registration 目录中。
├───blog                                   
│   │   admin.py                           
│   │   apps.py                            
│   │   forms.py                           
│   │   models.py                          
│   │   tests.py                           
│   │   urls.py                            
│   │   views.py                           
│   │   __init__.py                        
│   │                                      
│   ├───migrations                         
│   │                                      
│   ├───static                             
│   │   └───...                
│   │                                      
│   ├───templates                          
│   │   │   ...      
│   │   │                                  
│   │   ├───blog                           
│   │   │     ...              
│   │   │                                  
│   │   └───registration                   <-------
│   │           home.html                  
│   │           login.html      
|   |           password_reset_form.html      <------
|   |           password_reset_email.html     <------         
│   │           password_reset_done.html      <------
│   │           password_reset_confirm.html   <------
│   │           password_reset_complete.html  <------

请确保将模板命名为:

password_reset_form.html
password_reset_email.html
password_reset_done.html
password_reset_confirm.html
password_reset_complete.html

urls.py的路径应该如下所示:

from django.contrib.auth import views as auth_views
# and other imports ....

path(
        'password_reset',
        auth_views.PasswordResetView.as_view(template_name='registration/password_reset_form.html'),
        name='password_reset'
    ), 
...

2
我为此苦苦挣扎了几个小时。我试着重新编写管理模板,但最终还是决定不这样做,以免每次启动新项目都要重写它。
最终,在一篇引用Django 2.1文档的博客中找到了答案:"http://discuss.hellowebapp.com/t/django-2-1-changes/618/4"。 该博客解释了路径格式和视图语法的更改。

文件结构

###`blog/templates/registration/reset_password_form.html`###
###`blog/templates/registration/reset_password_done.html`###
###`blog/templates/registration/reset_password_complete.html`###
###`blog/templates/registration/reset_password_confirm.html`###
C:.                                                                   
├───blog                                   
│   │   admin.py                           
│   │   apps.py                            
│   │   forms.py                           
│   │   models.py                          
│   │   tests.py                           
│   │   urls.py                            
│   │   views.py                           
│   │   __init__.py                        
│   │                                      
│   ├───migrations                         
│   │                                      
│   ├───static                             
│   │   └───css                            
│   │       │   blog.css                   
│   │       │                              
│   │       └───static                     
│   │           └───images                 
│   │                                      
│   ├───templates                          <-------
│   │   │   add_comment_to_post.html       
│   │   │                                  
│   │   ├───blog                           
│   │   │       base.html                  
│   │   │       postDraftlist.html         
│   │   │       postEdit.html              
│   │   │       postsDetail.html           
│   │   │       postsList.html             
│   │   │                                  
│   │   └───registration                   <-------
│   │           home.html                  
│   │           login.html                 
│   │           password_reset_complete.htm    <---
│   │           password_reset_confirm.html    <---
│   │           password_reset_done.html       <---
│   │           password_reset_form.html       <---
│   │                                      
│   └───__pycache__                        
├───blogApp                                
│   │   settings.py                        
│   │   urls.py                            
│   │   wsgi.py                            
│   │   __init__.py                        
│   │                                      
│   └───__pycache__                            
│                                          
└───sent_emails                            
|          
│___.gitignore                             
│___db.sqlite3                             
│___manage.py                              
│              

.

from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
from django.contrib.auth.views import PasswordResetView, PasswordResetDoneView, PasswordResetConfirmView, PasswordResetCompleteView

urlpatterns = [
    path('admin/', admin.site.urls),
    #127.0.0.1:8000
    path('', include('blog.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('', TemplateView.as_view(template_name='home.html'), name='home'),
    path('accounts/password/reset/', PasswordResetView.as_view(template_name='registration/password_reset_form.html'), name='password_reset'),
    path('accounts/password/reset/', PasswordResetDoneView.as_view(template_name='registration/password_reset_done.html'), name='password_reset_done'),
    path('accounts/password/reset/', PasswordResetConfirmView.as_view(template_name='registration/password_reset_confirm.html'), name='password_reset_confirm'),
    path('accounts/password/reset/', PasswordResetCompleteView.as_view(template_name='registration/password_reset_comlete.html'), name='password_reset_complete'),

设置

INSTALLED_APPS =  [
    'blog',   #<--name of your app should go here at the top of this stack not bottom
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],

所以:

  1. 文件结构:templates/registration/password_reset_form.html
  2. urls.py: 导入和路径
  3. settings.py: INSTALLED_APPS 和 DIRS

(注:该文涉及IT技术,翻译仅供参考,如有不妥之处请谅解并指出)

1
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['accounts/templates/'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

请注意'DIRS'中的目录路径:[]...默认情况下,它是空的。放入'accounts/templates/'。同时,accounts是应用程序的名称,我假设你使用了accounts

1

前往Django管理注册目录

/home/username/Desktop/Project_folder/virtual_env_name/lib/python3.6/site-packages/django/contrib/admin/templates

现在打开 password_reset_form.html 文件,将

替换为
{% extends "admin/base_site.html" %}

从模板中,您想要扩展。在我的情况下,我执行以下操作

{% extends "feed/base.html" %}

源稿:feed: 应用名称 base.html: 基础文件


2
这意味着每次重新创建虚拟环境时,您都需要重新进行。 - Martin

1

使用path()在Django 2中自定义密码重置模板

基于@samschultz的回答

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['path/to/yor/templates/'], # Example: 'templates' or /myapp/templates'...
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

请注意 >>> 'DIRS' <<<。希望能对您有所帮助。

1
在尝试了本主题列出的所有方法后,我仍然无法使其工作。不过,通过将我的账户应用程序中的自定义 URL 替换为电子邮件显示的精确链接,我成功解决了问题。
#My original urls link:
'users/password_reset/<uidb64>/<token>/' 

#working link
'users/reset/<uidb64>/<token>/'

0

这对我有用,我将完整路径名作为我的模板名称包含了进去

  from django.contrib.auth import views as auth_views
# and other imports ....

path(
        'password_reset',
        auth_views.PasswordResetView.as_view(template_name='feed/templates/registration/password_reset_form.html'),
        name='password_reset'
    ), 
...

你的回答可以通过提供更多支持信息来改进。请进行[编辑]以添加更多详细信息,例如引用或文档,以便他人可以确认您的答案正确无误。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

0

模板路径应正确,否则无法正常工作。下面是适用于我的代码。

'DIRS': [os.path.join(BASE_DIR, 'projects/templates/projects')],

TEMPLATES = [

{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'projects/templates/projects')],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},

]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接