[Django]-Django admin custom template tag

5πŸ‘

βœ…

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 %}

πŸ‘€Mariusz Jamro

0πŸ‘

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 %}

Leave a comment