Version | 01 |
Release Date | 01 Jan 2021 |
Type | SAP Documents |
Status | Released |
Django has a unique way of handling static files , which are different based on environment (Development / Production).
Make sure that django.contrib.staticfiles
is included in your INSTALLED_APPS
.
2. In your settings file, define STATIC_URL
, for example:
STATIC_URL = '/static/'
3. In your templates, use the static
template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE
.
{% load static %} <img src="{% static 'my_app/example.jpg' %}" alt="My image"> Note : Dont add static tag to the base.html file. Should be part of the template being loaded 4. Store your static files in a folder calledstatic
in your app. For examplemy_app/static/my_app/example.jpg
.
Folder Structure
Create a folder static below the app folder at the same level as template folder
Create a subfolder with the name of the <app> and maintain all the static files. While rendering add the <app> to the path.
For development environment following additional setting required.
Make sure static is maintained as part of the urls.py file. This is not required for production environment.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)