Reputation: 16779
I'm currently overriding change_form.html
in my app by dropping it in:
myproject/myapp/templates/admin/change_form.html
That works fine but I really only want to override the change form for the User
model, and nothing else. It doesn't seem to work if I drop it in:
myproject/myapp/templates/admin/user/change_form.html
I'm guessing that's because the User
model isn't from my app?
What's the cleanest way of overriding change_list.html
for a specific model in some other app (namely, the django.contrib.auth
app)?
Relevant snippets from settings.py
:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
'unicorn.context_processors.site_url',
'unicorn.context_processors.consultant_data',
'unicorn.context_processors.branding',
],
'builtins': ['tracking.templatetags.tracking_extras'],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
},
},
]
Upvotes: 5
Views: 7507
Reputation: 888
for Django 3.2+ first you need to create a similar admin template structure directory in your project which is:
- project_dir
- app
- templates/admin/ <---
- venv
and then copy the change_form.html
from venv/lib/python[ver]/site-packages/dajngo/contrib/admin/templates/
into: /templates/admin/change_form.html
wich is the template folder in your project
this assuming if you using virtualenv
and then set settings.py
to:
import os
...
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates/')],
'APP_DIRS': True,
...
and then you can modify the /templates/admin/change_form.html
you already copy,
for example you only want show some text only for user model, you can do something like this:
look for line: {% block field_sets %}
and below that put:
{% if opts.model_name == "user" %} Put Your Text Here {% endif %}
so it's looks like this:
{% block field_sets %}
{% if opts.model_name == "user" %} Put Your Text Here {% endif %}
You can replace the user
with any model you want.
Upvotes: 0
Reputation: 103
4 years late, but anyone looking for an answer--
If you installed Grappelli package like me, your issue can be solved by going to python/lib/site-packages/grappelli
and looking for the change_form.html
there. This package overrides my custom template so I made changes to the grappelli file.
Upvotes: -1
Reputation:
looks like you need to add your application name to the full path for the template, so it should be
myproject/myapp/templates/admin/myapp/user/change_form.html
Upvotes: 4
Reputation: 156
settings.py
TEMPLATES = [
{
'BACKEND': ...,
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
...
}
]
Place admin/auth/user/change_form.html
in project's templates
directory.
Upvotes: 1