【问题标题】:How to override my template instead of django admin panel for reset password?如何覆盖我的模板而不是 django 管理面板以重置密码?
【发布时间】:2019-01-22 09:42:53
【问题描述】:

我正在关注此博客以重置 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 中添加更多内容 :) docs.djangoproject.com/en/2.1/topics/auth/default/…
  • 我现在已经完成了。我转到管理员 /home/imsaiful/Desktop/local_repo/myvenv/lib/python3.6/site-packages/django/contrib/admin/templates 的根目录并更改基本扩展文件的名称。感谢您的帮助.
  • 很遗憾,这都是过时的语法。

标签: python django python-3.x django-templates django-views


【解决方案1】:

您还可以确保您的应用在 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 替换为您的应用名称

【讨论】:

  • 这将导致管理面板使用您的视图而不是管理视图。这不是一个好的解决方案,因为它会扭曲管理面板的用户体验。
  • 将您的应用程序移到 INSTALLED_APPS 之上将无济于事。我已经检查过了。
【解决方案2】:

在您的 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 指向我的模板?模板目录放在哪里?
  • 是的,'DIRS' 是纠正该问题的正确位置。所以,在这种情况下,它应该是: 'DIRS': [os.path.join(BASE_DIR, 'feed', 'templates')],
【解决方案3】:

我为此苦苦挣扎了好几个小时。我尝试重写我认为反对它的管理模板,这样我就不必为每个新项目都重写它。

终于在引用 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

【讨论】:

    【解决方案4】:

    在 django 2 中使用带有path() 的自定义密码重置模板

    基于@samschultz response:

    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'

    希望对你有帮助。

    【讨论】:

      【解决方案5】:
      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。

      【讨论】:

        【解决方案6】:

        在尝试了此线程中列出的所有尝试后,我仍然无法使其正常工作。但是,通过将我的帐户应用程序中的自定义 URL 替换为它在电子邮件中显示的确切链接;我设法修复它。

        #My original urls link:
        'users/password_reset/<uidb64>/<token>/' 
        
        #working link
        'users/reset/<uidb64>/<token>/'
        

        【讨论】:

          【解决方案7】:

          要覆盖管理员模板以重置密码,文件夹结构需要遵循并记住将自定义模板放在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'
              ), 
          ...
          

          【讨论】:

            【解决方案8】:

            进入Django admin注册目录

            /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:基础文件

            【讨论】:

            • 这意味着每次重新创建虚拟环境时都要重做
            【解决方案9】:

            模板路径必须正确,否则将无法正常工作。下面对我有用。

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

            模板 = [

            {
                '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',
                    ],
                },
            },
            

            ]

            【讨论】:

              猜你喜欢
              • 2013-01-23
              • 2019-05-02
              • 2015-10-18
              • 2017-01-01
              • 2018-10-22
              • 2020-11-28
              • 2017-02-19
              • 1970-01-01
              • 2018-02-01
              相关资源
              最近更新 更多