Django Admin templates not being overwritten by app templates

29 views
Skip to first unread message

Alexander Beach

unread,
Nov 10, 2019, 3:49:32 PM11/10/19
to Django users
I am trying to create registration forms using Django's built in auth forms.  However, when using the Django admin, the Django admin templates are always being used.

My application structure is as follows:

├── bug
  ├── __init__.py
  ├── __pycache__
  ├── settings.py
  ├── urls.py
  └── wsgi.py
├── db.sqlite3
├── manage.py
└── mysite
   
├── admin.py
   
├── apps.py
   
├── __init__.py
   
├── migrations
   
  └── __init__.py
   
├── models.py
   
├── registration
   
├── templates
   
  └── registration
   
      └── password_reset_form.html
   
├── tests.py
   
└── views.py



My urls.py is the following:


from django.contrib import admin
from django.conf.urls import include, url
from django.urls import path


urlpatterns
= [
    path
('admin/', admin.site.urls),
    url
(r'^accounts/', include('django.contrib.auth.urls'), name="auth"),
]



The mysite/templates/registration/password_reset_form.html is not being rendered.  When I navigate to localhost:8000/accounts/password_reset, i can see that its using the Django Admin template. See attached screenshot.  I am expecting my custom template for the reset form to be rendered.

I am using Django version 2.2.5 and python 3.7.3

Is this a bug or the expected behavior? I don't want to remove the admin app. 




Screenshot from 2019-11-10 14-48-17.png

Mike Dewhirst

unread,
Nov 10, 2019, 4:51:00 PM11/10/19
to django...@googlegroups.com
On 11/11/2019 7:02 am, Alexander Beach wrote:
> I am trying to create registration forms using Django's built in auth
> forms.  However, when using the Django admin, the Django admin
> templates are always being used.
>
> My application structure is as follows:
>
> |
> ├──bug
> │├──__init__.py
> │├──__pycache__
> │├──settings.py
> │├──urls.py
> │└──wsgi.py
> ├──db.sqlite3
> ├──manage.py
> └──mysite
> ├──admin.py
> ├──apps.py
> ├──__init__.py
> ├──migrations
> │└──__init__.py
> ├──models.py
> ├──registration
> ├──templates
> |
| mysite/templates/admin/password_reset_form.html

Not tested but that's the principle. Django will default to using the
template from your site rather than its own. The admin is one of your
apps so it will use any templates it finds (that urls.conf knows about)
in your main templates directory.

Cheers

Mike

|
> |
> │└──registration
> │└──password_reset_form.html
> ├──tests.py
> └──views.py
>
> |
>
>
> My urls.py is the following:
>
>
> |
> fromdjango.contrib importadmin
> fromdjango.conf.urls importinclude,url
> fromdjango.urls importpath
>
>
> urlpatterns =[
>     path('admin/',admin.site.urls),
>     url(r'^accounts/',include('django.contrib.auth.urls'),name="auth"),
> ]
>
> |
>
>
> The mysite/templates/registration/password_reset_form.html is not
> being rendered.  When I navigate to
> localhost:8000/accounts/password_reset, i can see that its using the
> Django Admin template. See attached screenshot.  I am expecting my
> custom template for the reset form to be rendered.
>
> I am using Django version 2.2.5 and python 3.7.3
>
> Is this a bug or the expected behavior? I don't want to remove the
> admin app.
>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c718299d-53f6-4e56-8976-a2040f3449ac%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/c718299d-53f6-4e56-8976-a2040f3449ac%40googlegroups.com?utm_medium=email&utm_source=footer>.

Alexander Beach

unread,
Nov 10, 2019, 5:16:27 PM11/10/19
to Django users
This behavior seems counter intuitive, and contrary to what the documentation says

For example: my settings.py file is as follows:

TEMPLATES = [
   
{
       
'BACKEND': 'django.template.backends.django.DjangoTemplates',
       
'DIRS': [],
       
'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',
           
],
       
},
   
},
]


According to this documentation, APP_DIR should behave as follows:

https://docs.djangoproject.com/en/2.2/howto/overriding-templates/#overriding-from-an-app-s-template-directory

With APP_DIRS set to True, the template loader will look in the app’s templates directory and find the templates.

This isn't the behavior that is happening. djang_admin and mysite are technically separate apps, and have seperate template directories. 

Aldian Fazrihady

unread,
Nov 10, 2019, 6:17:44 PM11/10/19
to django...@googlegroups.com
You need to reorder the app list in INSTALLED_APP setting, so `django.contrib.admin` is located lower than your app`.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c718299d-53f6-4e56-8976-a2040f3449ac%40googlegroups.com.


--
Regards,

Reply all
Reply to author
Forward
0 new messages