- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Making your own custom filter tags in Django
Filters are really very helpful, there are many built-in filters in Django. We can also make our own filters in Django which we can use in Django projects in any template file. Filters are used in many ways like making lowercase to uppercase in HTML.
In this article, we will see how to make a custom template filter tag in Django. So, let's get started.
Example
First of all, create a Django project and an app.
Add a templates and templatetags directory in your apps root project. The project structure would look like this −
example is our app and tutorial is our project.
Make sure your TEMPLATES variable in settings.py looks like this −
import os TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTem plates', 'DIRS': [os.path.join(BASE_DIR, 'example/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', ], }, }, ]
Add "example" to INSTALLED_APPS variable of settings.py. It just sets up the templates folder.
In the templates folder, add a home.html and leave it like that for now.
In views.py of example, add the following lines −
from django.shortcuts import render # Create your views here. def main(request): return render(request,"home.html")
Here we simply rendered our main frontend in main view.
In urls.py of the project, add the following lines −
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('',include('example.urls')), path('admin/', admin.site.urls), ]
Here we included our apps urls.py in main urls.
In urls.py of example or app, add the following −
from django.urls import path,include from . import views urlpatterns = [ path('', views.main,name="main") ]
It sets up the basic path urls and render our main view.
Create __init__.py in templatetags folder and a filter file named "upperfilter.py". Add the following code in it −
from django.template import Library register=Library() @register.filter def upper(value): return value.upper()
Here we imported a Library and registered it. We created a function or we can say a filter named upper and we registered it using decorator. Each filter takes some value; we can define how many values it can take.
Come back to your home.html and add −
<!DOCTYPE html> <html> <head> <title>Tut</title> </head> <body> {% load upperfilter %} <h1>{{"hi friends how are you" | upper}}</h1> </body> </html>
Here on the frontend, we loaded our filter using {% %}, and then in <h1> element, we used the filter.
This filter will convert lowercase to uppercase.
Output
Without filter −
With filter −
- Related Articles
- How to Build your own website using Django in Python
- How to humanize tags in Django?
- Making a Pickle field in Django models
- Making a URL shortner app in Django
- Django – Making a Django website more human-like using Humanizer
- How to implement django-material in your Django project?
- Making a simple counter app using request.session in Django
- Build Your Own Botnet
- Write your own memcpy() in C
- Implement your own itoa() in C
- Write your own atoi() in C++
- How to create our own/custom functional interface in Java?\n
- Do you need text/javascript specified in your tags?
- Can you build your own sundial?
- How will implement Your Own sizeof in C
