{% static %} नामक Template Tag
{% 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.
{% 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.
{% 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.
{% 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. #}
- {% static %} उपयोग करने से पहले template के top पर {% load static %} भूल जाना, जो TemplateSyntaxError का कारण बनता है।
- एक absolute path जैसे {% static '/style.css' %} pass करना, बजाय एक static/ directory के सापेक्ष path के।
- 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 के नहीं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: