Template के Filters
In this page:
{{ variable|filter_name }}
{{ variable|filter_name:argument }}
एक Basic Filter Apply करना
Pipe character | इसके left वाले variable पर एक filter apply करता है, print होने से पहले output transform करते हुए।
उदाहरण: Applying a Basic Filter
|upper converts the string to uppercase; |length outputs the number of characters in the string.
<!-- context = {'title': 'django templates'} -->
<h1>{{ title|upper }}</h1>
<p>{{ title|length }} characters</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Arguments के साथ Filters
कुछ filters एक argument accept करते हैं, एक colon के बाद लिखा जाता है, जैसे text को words की एक set number तक truncate करना।
उदाहरण: Filters with Arguments
truncatewords:5 cuts the text down to the first 5 words followed by an ellipsis.
<!-- context = {'article': 'Django makes building web applications fast and enjoyable for developers.'} -->
<p>{{ article|truncatewords:5 }}</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
कई Filters Chain करना
Filters कई pipes के साथ chain किए जा सकते हैं; हर filter पिछले वाले का output प्राप्त करता है, left से right apply होते हुए।
उदाहरण: Chaining Multiple Filters
|lower converts to lowercase first, then |trim removes the surrounding whitespace from that lowercased result.
<!-- context = {'bio': ' DJANGO DEVELOPER '} -->
<p>{{ bio|lower|trim }}</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
default के साथ Fallback Values देना
default filter एक fallback value output करता है जब variable falsy हो (empty string, None, 0, या missing)।
उदाहरण: Providing Fallback Values with default
Since nickname is an empty string (falsy), the filter substitutes friend instead.
<!-- context = {'nickname': ''} -->
<p>Hello, {{ nickname|default:"friend" }}!</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Variable और filter name के बीच pipe symbol | भूल जाना, जैसे {{ name|upper }} के बजाय {{ name upper }} लिखना।
- Filters को गलत order में chain करना और वही result expect करना -- filter order मायने रखता है क्योंकि हर एक पिछले output को transform करता है।
- एक colon के बिना एक filter argument pass करने की कोशिश करना, जैसे {{ value|truncatewords:10 }} के बजाय {{ value truncatewords 10 }} लिखना।
- Filters pipe syntax उपयोग करके एक variable का output transform करते हैं: {{ variable|filter }}।
- कुछ filters एक colon के बाद एक argument लेते हैं, जैसे {{ text|truncatewords:30 }}।
- Common built-in filters में upper, lower, date, length, default, और truncatewords शामिल हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: