[Fixed]-How to override my template instead of django admin panel for reset password?

1πŸ‘

βœ…

Go to the Django admin registration directory

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

Now open the password_reset_form.html and replace the

{% extends "admin/base_site.html" %}

from the template, you want to extend. In my case, I do the following

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

feed: app name
base.html: base file

πŸ‘€imsaiful

23πŸ‘

You can also make sure that your app comes before all other Django apps in INTALLED_APPS
e.g

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

replace your_app_name with the name of your app

πŸ‘€samschultz

5πŸ‘

In your settings.py make sure your TEMPLATES settings is equal to the following

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',

        ],
    },
},]

the most important part here is the DIRS

πŸ‘€samschultz

2πŸ‘

I struggled with this for hours. I tried rewriting the admin template I thought against it so I didn’t have to rewrite it for every new project.

finally stumbled across the answer on a blog referencing the Django 2.1 docs: β€œhttp://discuss.hellowebapp.com/t/django-2-1-changes/618/4β€œ.
The blog explains the changes to path formatting and views syntax.

FILE STRUCTURE

###`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'),

SETTINGS

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')],

So:

  1. file structure:templates/registration/password_reset_form.html
  2. urls.py: imports and paths
  3. settings.py: INSTALLED_APPS and DIRS
πŸ‘€Tucker Wray

2πŸ‘

To override the admin templates for a password reset, the folder structure needs to be following and remember to place the custom templates in templates/registration directory.

β”œβ”€β”€β”€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  <------

And be sure to name the template as,

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

And urls.py paths should look like,

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'
    ), 
...

1πŸ‘

Use custom password reset templates in django 2 with path()

Based on @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',
            ],
        },
    },
]

Please, pay attention to >>> β€˜DIRS’ <<<.

Hope it helps.

πŸ‘€mrroot5

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

Take note of the directory path inside β€˜DIRS’: []…By default, it is empty. Put 'accounts/templates/'. Also the accounts is the name of the app, which I assumed that you used accounts.

1πŸ‘

After trying all the attempts listed in this thread, I still couldn’t get it to work. However, by replacing my custom urls in my accounts app to the exact link it was displaying in the email; I managed to fix it.

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

#working link
'users/reset/<uidb64>/<token>/'
πŸ‘€lkal

0πŸ‘

Path of templates should be correct else it will not work. Below works for me.

β€˜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',
        ],
    },
},

]

0πŸ‘

This worked for me, I included the full path name as my template name

  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'
    ), 
...

Leave a comment