213👍
Besides putting my_templatetag.py
inside app_name/templatetags
, make sure you restart the Django development server (or ensure it restarted itself) every time you modify template tags. If the server does not restart, Django won’t register the tags.
111👍
From Django 1.9, you can load those new tags/filters in settings like this:
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',
'app.apptemplates.load_setting',
],
'libraries':{
'my_templatetag': 'app.templatetags.my_templatetag',
}
},
},
]
If you have templatetag dir in your project dir (not in an app dir), then above method is recommended.
Example-
- [Django]-How to resize an ImageField image before saving it in python Django model
- [Django]-Select DISTINCT individual columns in django?
- [Django]-How to solve "Page not found (404)" error in Django?
53👍
Make sure you are not missing any of the following steps:
-
Create a folder called “templatetags” at the same level as models.py
and views.py in your application folder -
Your application must be in the INSTALLED_APPS in settings.py
-
The templatetags folder must have __init__.py
-
Restart the django server
- [Django]-DatabaseError: current transaction is aborted, commands ignored until end of transaction block?
- [Django]-Table thumbnail_kvstore doesn't exist
- [Django]-Django Cache cache.set Not storing data
27👍
In my case the problem was, I was using {% load filter_method_name %}
I had to change to {% load filename %}
I then had to restart the server.
- [Django]-Django admin ManyToMany inline "has no ForeignKey to" error
- [Django]-Adding css class to field on validation error in django
- [Django]-Http POST drops port in URL
10👍
you have to manually stop the development server and start it again,
so Django can identify the new template tags
- [Django]-Django: Fat models and skinny controllers?
- [Django]-Django models: get list of id
- [Django]-Whats the difference between using {{STATIC_URL}} and {% static %}
5👍
Where is ‘my_templatetag.py’ stored? It should be stored in a directory called ‘templatetags’ which is within the app.
Please see: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
if that isn’t the case.
- [Django]-Retrieving a Foreign Key value with django-rest-framework serializers
- [Django]-What's the purpose of Django setting ‘SECRET_KEY’?
- [Django]-Converting Django QuerySet to pandas DataFrame
5👍
I am using Django 1.11, and I was having the same problem. Some of the answers here are right, but some things may be missing. Here is what I did:
Quoting a previous user:
Create a folder called “templatetags” at the same level as models.py
and views.py in your application folderYour application must be in the INSTALLED_APPS in settings.py
The templatetags folder must have init.py
But, before you re-start the Django server, add this to the file that contains the tags:
from django import template
register = template.Library()
Then you can re-start the server.
- [Django]-Django switching, for a block of code, switch the language so translations are done in one language
- [Django]-ManyRelatedManager object is not iterable
- [Django]-How to implement followers/following in Django
4👍
Restart the django server. It worked for me after setting the templatetag folder within the app and template_name.py in the templatetag folder.
- [Django]-Django stops working with RuntimeError: populate() isn't reentrant
- [Django]-How can I access environment variables directly in a Django template?
- [Django]-Django Model Field Default Based Off Another Field in Same Model
3👍
In case it helps someone, the issue in my case was that I was using quotes when trying to load the tag(s)
{% load 'my_templatetag' %} <!-- incorrect -->
instead of
{% load my_templatetag %} <!-- correct -->
Note: extends
needs quotes around the filename but not load
- [Django]-Cron and virtualenv
- [Django]-CORS error while consuming calling REST API with React
- [Django]-Get list item dynamically in django templates
1👍
I know this is a bit old, but I ran into the same problem today. I found the solution in the docs: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
The app should contain a templatetags directory, at the same level as models.py, views.py, etc. If this doesn’t already exist, create it – don’t forget the __init__.py file to ensure the directory is treated as a Python package.
Simply copying the __init__.py from another location into the new templatetag’s directory sorted it out.
- [Django]-How do I perform HTML decoding/encoding using Python/Django?
- [Django]-Handle `post_save` signal in celery
- [Django]-405 "Method POST is not allowed" in Django REST framework
1👍
I solved this by adding a templatestag folder in the root with a filter.py file defining my filters, then I adjusted my settings.py.
Please check my complete answer regarding this issue in this similar thread
- [Django]-CORS error while consuming calling REST API with React
- [Django]-Rendering a value as text instead of field inside a Django Form
- [Django]-Python + Django page redirect
1👍
For me, I had to register my customer Filter as this since my templatetags are outside of any app
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
'django.template.context_processors.media',
],
# ! New Line
'libraries':{
'customFilter': 'templatetags.customFilter',
}
},
},
]
- [Django]-Django delete FileField
- [Django]-How to concatenate strings in django templates?
- [Django]-Django: How to manage development and production settings?
0👍
put my_templatetag.py inside app_name/templatetags then create init.py inside app_name/templatetags .. Then open terminal in project folder give command python manage.py shell
from app_name.templatetags import my_templatetag
- [Django]-How to debug in Django, the good way?
- [Django]-Django self-referential foreign key
- [Django]-Table thumbnail_kvstore doesn't exist
0👍
you just cut/remove your code which written inside the (example templatetags/home.py)
from home.py you remove your code and restart your server and again paste your code in home.py it will work.
- [Django]-How to get superuser details in Django?
- [Django]-Import data from excel spreadsheet to django model
- [Django]-Django-Bower + Foundation 5 + SASS, How to configure?
0👍
The templatetags folder must have a __init__.py file in order to be a regular python package
Ensure that you also created the templatetags.py file next to the __init__.py file
- [Django]-How to add new languages into Django? My language "Uyghur" or "Uighur" is not supported in Django
- [Django]-How can I disable logging while running unit tests in Python Django?
- [Django]-Django render_to_string missing information
0👍
This Solved My Problem:
- make sure templatetags folder exsist in main django project
- use this line in INSTALLED_APPS in project settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
...
'my_app.apps.AppConfig',
]
- [Django]-How do I use an UpdateView to update a Django Model?
- [Django]-How to upload a file in Django?
- [Django]-Django – Circular model import issue
0👍
I know this is old, but in your case you should try
{% load has_group from my_templatetag %}
- [Django]-Django logging of custom management commands
- [Django]-Remove pk field from django serialized objects
- [Django]-Create a field whose value is a calculation of other fields' values
-1👍
at first stop the server.remove/cut the code from templatetags/tag.py and rewrite/paste it.then run the server.it worked for me
- [Django]-TransactionManagementError "You can't execute queries until the end of the 'atomic' block" while using signals, but only during Unit Testing
- [Django]-Django change default runserver port
- [Django]-Get Timezone from City in Python/Django
-2👍
Yah, This problem you are currently facing because of older django version Or Complexly to write "Depreciation"
if You HAve These Types OF TAgs In Your template/ Html Files Change Them With..
> {{% load staticfiles %} or {% load admin_static %}, {% load
> admin_static %}}
change with
{% load static %}
Get to The Point..
JUst SImply Perform These Replace All These from YOur BAse.html/or Any type Of HTML
- [Django]-How to change empty_label for modelForm choice field?
- [Django]-405 "Method POST is not allowed" in Django REST framework
- [Django]-Get class name of django model