← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 5 of 10

Template के Filters

Filters छोटी machines जैसे हैं जिनसे आप एक value को | symbol से pipe करते हैं इसे clean up करने या print होने से पहले इसका look बदलने के लिए।
Syntax
markup
{{ 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.

markup
<!-- 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.

markup
<!-- 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.

markup
<!-- 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.

markup
<!-- 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. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Variable और filter name के बीच pipe symbol | भूल जाना, जैसे {{ name|upper }} के बजाय {{ name upper }} लिखना।
  2. Filters को गलत order में chain करना और वही result expect करना -- filter order मायने रखता है क्योंकि हर एक पिछले output को transform करता है।
  3. एक 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 शामिल हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.