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

STATIC_URL Configure करना

STATIC_URL वह web address prefix है जो आपका browser pictures और style sheets मांगने के लिए उपयोग करता है, एक street name जैसा जो mail carrier को बताता है कि किस neighborhood में deliver करना है।
Syntax
markup
# settings.py
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'

settings.py में STATIC_URL Set करना

STATIC_URL एक required setting है जो Django (और browser) को बताती है कि static file requests को कौन सा URL prefix उपयोग करना चाहिए।

Note: एक नया Django project इसे default रूप से 'static/' set करता है — आपको इसे शायद ही कभी बदलना पड़े।

उदाहरण: Setting STATIC_URL in settings.py

STATIC_URL is a required setting that tells Django (and the browser) what URL prefix static file requests should use.

markup
# settings.py
STATIC_URL = 'static/'
{# 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_URL vs STATIC_ROOT

STATIC_URL public-facing URL prefix है; STATIC_ROOT वह absolute filesystem path है जहां collectstatic production serving के लिए files copy करता है। ये अलग purposes serve करते हैं और deployment के लिए दोनों चाहिए।

Warning: STATIC_ROOT को कभी भी एक ऐसी directory के बराबर set न करें जो आप STATICFILES_DIRS में भी list करते हैं — collectstatic source files overwrite कर सकता है।

उदाहरण: STATIC_URL vs STATIC_ROOT

STATIC_URL is the public-facing URL prefix; STATIC_ROOT is the absolute filesystem path collectstatic copies files into for production serving. They serve different purposes and are both needed for deployment.

markup
# settings.py
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
{# 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_URL को सुरक्षित रूप से Reference करना

Template में directly /static/ type करने के बजाय, {% static %} उपयोग करें ताकि URL सही रहे भले ही STATIC_URL बाद में बदल जाए।

उदाहरण: Referencing STATIC_URL Safely

Rather than typing /static/ directly in a template, use {% static %} so the URL stays correct even if STATIC_URL changes later.

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. #}
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_URL को trailing slash के बिना set करना, /staticstyle.css जैसे malformed URLs produce करते हुए।
  2. STATIC_URL (वह URL prefix जो browsers उपयोग करते हैं) को STATIC_ROOT (server-side folder जहां collectstatic लिखता है) से confuse करना।
  3. Templates के पहले से /static/ paths hardcode करने के बाद STATIC_URL बदलना बजाय {% static %} tag उपयोग करने के।
चैप्टर सारांश
  • STATIC_URL वह URL prefix set करता है जो browser में static files reference करने के लिए उपयोग होता है।
  • यह एक trailing slash के साथ खत्म होना चाहिए, जैसे 'static/'।
  • STATIC_URL STATIC_ROOT से अलग है, जो सिर्फ deployment के दौरान उपयोग होने वाला एक filesystem path है।
  • Templates को STATIC_URL को indirectly {% static %} tag के through reference करना चाहिए बजाय इसे hardcode करने के।

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.