imsaiful
imsaiful

Reputation: 1604

How to override my template instead of django admin panel for reset password?

I am following this blog to reset the user password in Django. It is working perfectly. But the problem is that I want to show my template instead of the Django admin panel when resetting the password or confirming the mail. How can I achieve it?

This is my urls.py file

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

What step do I need to take for template and views> I have tried many and add some file like:

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

But there is no effect> I just want to render my website template while resetting the password.

This is my directory structure:

├── 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

Thanks!

Upvotes: 14

Views: 9132

Answers (10)

Fredrick Ochieng
Fredrick Ochieng

Reputation: 1

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

Upvotes: 0

Avishka Dambawinna
Avishka Dambawinna

Reputation: 1215

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

Upvotes: 2

Mohd Ahshan Danish
Mohd Ahshan Danish

Reputation: 141

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

]

Upvotes: 0

lkal
lkal

Reputation: 25

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

Upvotes: 1

Tucker Wray
Tucker Wray

Reputation: 31

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

Upvotes: 2

Iyanuoluwa Ajao
Iyanuoluwa Ajao

Reputation: 705

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.

Upvotes: 1

mrroot5
mrroot5

Reputation: 1971

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.

Upvotes: 1

imsaiful
imsaiful

Reputation: 1604

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

Upvotes: 0

samschultz
samschultz

Reputation: 359

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

Upvotes: 27

samschultz
samschultz

Reputation: 359

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

Upvotes: 5

Related Questions