TemPlate Inheritance Filters
TemPlate Inheritance Filters
TemPlate Inheritance Filters
======================================
1).Template Inheritance
2).Template Filters
Template Inheritance:
--------------------
-->If multiple template files have same common code, it is not recommended
to write that common code in every template html file.
It increases length of the code and reduces readability. It is also increases
development time.
-->We have to separate that common code into a new template file, which is also
known as base template.
The remaining template files should required to extend base templates so that
common code will be inherited
automatically.
-->Inheriting common code from base template to remaining templates is nothing but
template inheritance.
child.html
----------
<!DOCTYPE html>
{% extends 'testapp/base.html' %}
{% block child_block %}
Child specific code
{% endblock %}
base.html
---------
<body>
<nav class='navbar'>
<div class="container">
<a class ='navbar' href="/">Babu News</a>
<ul class ='navbar-nav'>
<li><a class ='navbar' href="#">Movies</a></li>
<li><a class ='navbar' href="#">Sports</a></li>
<li><a class ='navbar' href="#">Politics</a></li>
</ul>
</div>
</nav>
{% block body_block %}
{% endblock %}
</body>
views.py
--------
def base_view(request):
return render(request,'testapp/base.html')
urls.py
-------
path('',views.base_view)
movie.html
----------
<!DOCTYPE html>
{% load static %}
{% extends 'testapp/base.html' %}
{% block body_block %}
<h1>This is movies information</h1>
{% endblock %}
views.py
-------
def movie_view(request):
return render(request,'testapp/movie.html')
urls.py
------
path('movie/',views.movie_view)
sports.html
-----------
<!DOCTYPE html>
{% extends 'testapp/base.html' %}
{% load static %}
{% block body_block %}
<h1>This is sports information</h1>
{% endblock %}
views.py
--------
def sports_view(request):
return render(request,'testapp/sports.html')
urls.py
-------
path('sports/',views.movie_view)
politics.html
-------------
<!DOCTYPE html>
{% extends 'testapp/base.html' %}
{% load static %}
{% block body_block %}
<h1>This is politics information</h1>
{% endblock %}
views.py
--------
def politics_view(request):
return render(request,'testapp/politics.html')
urls.py
-------
path('politics/',views.politics_view)
Note:
-------
1).In the parent template, we can define any number of blocks.
But child template is responsible to implement these blocks.
2).It is not mandatory to implement every parent block in child block.
Based on our requirement, child template can decide which blocks are required to
implement.
3).While implementing blocks in child template, it is not required to follow order.
4).Child template can extend any number of parent templates. i.e multiple
inheritance is allowed.
Template Filters:
--------------------------
-->In the template file, the injected data can be displayed by using template tags.
{{name}} {{emp.eno}}
filterproject:
-------------
D:\Babu_Tem_Inher_Filters>django-admin startproject filterproject
D:\Babu_Tem_Inher_Filters>cd filterproject
D:\Babu_Tem_Inher_Filters\filterproject>py manage.py startapp testapp
models.py
--------
class FilterModel(models.Model):
name = models.CharField(max_length=30)
subject = models.CharField(max_length=30)
dept = models.CharField(max_length=30)
date = models.DateField()
admin.py
-------
from testapp.models import FilterModel
class FilterModelAdmin(admin.ModelAdmin):
list_display = ['name','subject','dept','date']
admin.site.register(FilterModel,FilterModelAdmin)
views.py
-------
from testapp.models import FilterModel
def upper_data_view(request):
records = FilterModel.objects.all()
return render(request,'testapp/upperdata.html',{'records':records})
upperdata.html
--------------
<body>
{% for record in records %}
<h1>{{record.name}} Information</h1>
<ul>
<li>Name:{{record.name}}</li>
<li>Subject:{{record.subject}}</li>
<li>Dept:{{record.dept}}</li>
<li>Date:{{record.date}}</li>
</ul><hr>
{% endfor %}
</body>
urls.py
----------
path('upperview/',views.upper_data_view)
filters:
-------
<li>Name:{{record.name | upper}}</li>
<li>Subject:{{record.subject | lower}}</li>
<li>Dept:{{record.dept | title}}</li>
for date:
------------
<li>Date:{{record.date | date:"d-m-Y"}}</li>
<li>Date:{{record.date | date:"m-d-Y"}}</li>
<li>Date:{{record.date | date:"m/d/Y"}}</li>
<li>Date:{{record.date | timesince}}</li>
<li>Date:{{record.date | date:'d-b-Y'}}</li>
<li>Date:{{record.date | date:'l, F j, Y'}}</li>
steps:
--------
1).Create a folder 'templatetags' inside our application folder.
2).Create a special file named with '__init__.py' inside this folder(templatetags),
so that django will consider this folder as python package.
3).Create a python file inside this folder to define our filters cust_filters.py
cust_filters.py
---------------------
from django import template
register = template.Library()
def first_five_upper(value):
result = value[:5].upper()
return result
def first_n_upper(value,n):
result = value[:n].upper()
return result
register.filter('ffu',first_five_upper)
register.filter('fnu',first_n_upper)
html file:
--------------
<body>
{% load cust_filters %}
<li>Name:{{record.name | ffu}}</li>
<li>Name:{{record.name | fnu:3}}</li>
</body>