TemPlate Inheritance Filters

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Working with Advanced Template Features:

======================================
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.

How to implement template inheritance:


--------------------------------------
base.html:
---------
<body>
common code required for every child template
{% block chidld_block %}
Anything outside of this block available to child tag
In child template the specific code should be in this block
{% endblock %}
</body>

child.html
----------
<!DOCTYPE html>
{% extends 'testapp/base.html' %}
{% block child_block %}
Child specific code
{% endblock %}

How to add separate CSS files to child Templates?


-------------------------------------------------
D:\Babu_Tem_Inher_Filters>django-admin startproject advtemplateproject
D:\Babu_Tem_Inher_Filters>cd advtemplateproject
D:\Babu_Tem_Inher_Filters\advtemplateproject>py manage.py startapp testapp

-->Add app in settings.py

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.

Advantages of Template Inheritance:


-----------------------------------
1).What ever code available in base template is by default available to child
templates and we are not required to write again.
Hence it promotes code re-usability.
2).It reduces length of the ocde and improves readability.
3).It reduces development time.
4).It provides unique and same look and feel for total web application.

Template Filters:
--------------------------
-->In the template file, the injected data can be displayed by using template tags.
{{name}} {{emp.eno}}

-->Before displaying to the end user, if we want to perform some modifications to


the injected text, like add something or cut something, case conversions etc then
we should go for filetrs.

Syntax for Template Filter:


---------------------------
{{value | filtername }}
{{ value | filtername:'argument'}}
-->Argument to the filter are optional

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

-->Add app in settings.py

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()

-->Makemigrations and migrate

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>

How to create our own filters:


--------------------------------------------
-->based on our requirement, we can create own filters, if predefined filters are
not fullfill our requirement.

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>

You might also like