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

Templates में Static Files Link करना

{% static %} tag आपकी CSS, JavaScript, और image files के लिए सही web address बनाता है ताकि links कभी न टूटें।
Syntax
markup
{% load static %}

<link rel="stylesheet" href="{% static 'path/to/file.css' %}">
<img src="{% static 'path/to/image.png' %}" alt="description">

static Template Tag Load करना

{% static %} उपयोग करने से पहले, एक template को static tag library {% load static %} से load करना चाहिए, typically file के बिल्कुल top पर।

Warning: {% load static %} भूल जाना एक TemplateSyntaxError का कारण बनता है: static एक registered tag नहीं है।

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

{% load static %} makes the {% static %} tag available for the rest of this template.

markup
<!-- templates/base.html -->
{% load static %}
<!DOCTYPE html>
<html>
<head></head>
<body></body>
</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 path %} किसी भी app के static/ folder के अंदर एक file के लिए, STATIC_URL के साथ combined, सही URL पर resolve होता है।

उदाहरण: Linking a CSS File

This renders as href="/static/core/css/style.css" (or wherever STATIC_URL points), instead of a hardcoded path.

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

static के साथ एक Image Link करना

वही {% static %} tag किसी भी static asset के लिए काम करता है, <img> tags में reference किए गए images सहित।

उदाहरण: Linking an Image with static

The logo's URL is generated dynamically, so it stays correct even if STATIC_URL changes for production.

markup
{% load static %}
<img src="{% static 'core/images/logo.png' %}" alt="Site 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 %} उपयोग करने के बजाय एक static file path जैसे '/static/style.css' hardcode करना, जो STATIC_URL या deployment paths बदलने पर टूट जाता है।
  2. Template में कहीं भी {% static %} tag उपयोग करने से पहले top पर {% load static %} भूल जाना।
  3. Path के चारों ओर quotes गलत तरीके से लगाना, जैसे {% static css/style.css %} बिना quotes के, जो एक TemplateSyntaxError raise करता है।
चैप्टर सारांश
  • {% load static %} को एक template में {% static %} के किसी भी उपयोग से पहले appear होना चाहिए।
  • {% static 'path/to/file' %} STATIC_URL के आधार पर एक static asset के लिए सही URL generate करता है।
  • {% static %} उपयोग करना hardcoded paths के बजाय templates को development और production में correct रखता है।

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.