← Back to Django Course | Chapter 11: Static Files & Media Handling | Lesson 4 of 8

{% static %} नामक Template Tag

{% static %} tag एक librarian से किताब उसके title से मांगने जैसा है, बजाय यह याद रखने के कि वह किस shelf पर है — Django आपके लिए असली URL खुद ढूंढ लेता है।
Syntax
markup
{% load static %}

<link rel="stylesheet" href="{% static 'path/file.css' %}">
<script src="{% static 'path/file.js' %}"></script>

static Template Tag Load करना

किसी भी template में {% static %} उपयोग करने से पहले, {% load static %} top के पास लिखा होना चाहिए ताकि यह tag available हो सके।

उदाहरण: Loading the static Template Tag

Before {% static %} can be used anywhere in a template, {% load static %} must appear near the top to make the tag available.

markup
{% load static %}
<!DOCTYPE html>
<html>
<head></head>
</html>
{# 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. #}

एक CSS File Link करना

{% static %} एक static/ directory के सापेक्ष एक path लेता है और पूरा URL output करता है, इसे STATIC_URL के साथ अपने आप combine करते हुए।

उदाहरण: Linking a CSS File

{% static %} takes a path relative to a static/ directory and outputs the full URL, combining it with STATIC_URL automatically.

markup
{% load static %}
<link rel="stylesheet" href="{% static 'myapp/style.css' %}">
{# 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. #}

एक Image Link करना

वही tag किसी भी static asset के लिए काम करता है, सिर्फ CSS के लिए नहीं — images, JavaScript, और fonts सभी उसी तरह resolve होते हैं।

उदाहरण: Linking an Image

The same tag works for any static asset, not just CSS — images, JavaScript, and fonts all resolve the same way.

markup
{% load static %}
<img src="{% static 'myapp/logo.png' %}" alt="Logo">
{# 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. {% static %} उपयोग करने से पहले template के top पर {% load static %} भूल जाना, जो TemplateSyntaxError का कारण बनता है।
  2. एक absolute path जैसे {% static '/style.css' %} pass करना, बजाय एक static/ directory के सापेक्ष path के।
  3. Tag उपयोग करने के बजाय /static/style.css सीधे hardcode करना, जो break हो जाता है अगर STATIC_URL या एक CDN prefix बदले।
चैप्टर सारांश
  • {% load static %} template में {% static %} के किसी भी उपयोग से पहले आना चाहिए।
  • {% static 'path/to/file' %} STATIC_URL उपयोग करके अपने आप सही URL में resolve होता है।
  • Hardcoded paths के बजाय tag उपयोग करना templates को काम करते रहने देता है भले ही static file hosting बदल जाए।
  • {% static %} को pass किया गया path एक static/ directory के सापेक्ष है, filesystem root के नहीं।

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.