brsbilgic
brsbilgic

Reputation: 11833

Django admin custom template tag

I'm trying to customize my admin panel. I have copied change_list.html to the proper subfolder which is templates -> admin -> model -> change_list.html

While I customize the change_list.html, I'd like to use a templatetag but I'm not sure where to put my custom template tag library.

When I put it under django/contrib/admin/templatetags/, it works fine but I want to keep it in my own project tree.

Do you have any idea?

Note: I have also load my template tag in change_list.html as

{% load adminmedia admin_list i18n grp_tags myproject_tags %}

Thanks.

Upvotes: 3

Views: 3593

Answers (2)

The easiest way is to copy templatetags folder from /django/contrib/admin/templatetags in your virtual environment to core folder where settings.py is, then create custom_tags.py in templatetags folder as shown below, then don't forget to restart server to apply all .py files except __init__.py to Django project. *You can also override the code like the tags or filters in admin_list.py, admin_modify.py, admin_urls.py, base.py and log.py and you can see my answer explaining templatetags folder and load tag:

Django Project
 |-core
 |  |-settings.py
 |  └-templatetags # Here
 |     |-__pycache__
 |     |-__init__.py
 |     |-admin_list.py
 |     |-admin_modify.py
 |     |-admin_urls.py
 |     |-base.py
 |     |-log.py
 |     └-custom_tags.py # Here
 |-templates
 |  └-admin
 |     └-model
 |        └-change_list.html
 |-app1
 └-app2

Then in change_list.html, load the custom tags in custom_tags.py as shown below:

# "/templates/admin/model/change_list.html"

                                         # ↓ Here ↓
{% load i18n admin_urls static admin_list custom_tags %}

Upvotes: 0

Mariusz Jamro
Mariusz Jamro

Reputation: 31663

Do not modify or add anything to directory containing Django (do not modify Django!). Keep everything in your project directory (like in the manual).

Admin templates are exactly the same as non-admin templates and you use custom template tags exactly the same way. Put your template tags in yourapp/templatetags/ directory. If your app is in the settings.INSTALLED_APPS that you can load it's tags by passing the module name to the load tag. It accepts also package.module syntax, so: {% load somelibrary %} or {% load package.otherlibrary %}

Upvotes: 5

Related Questions