Templates में Static Files Link करना
{% 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 पर।
उदाहरण: Loading the static Template Tag
{% load static %} makes the {% static %} tag available for the rest of this template.
<!-- 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.
{% 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.
{% 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. #}
- {% static %} उपयोग करने के बजाय एक static file path जैसे '/static/style.css' hardcode करना, जो STATIC_URL या deployment paths बदलने पर टूट जाता है।
- Template में कहीं भी {% static %} tag उपयोग करने से पहले top पर {% load static %} भूल जाना।
- 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 रखता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: