Configure Static & Media Files in Django | Python

Опубликовано: 11 Октябрь 2024
на канале: SkillsHats
7,465
55

In this video, We will learn how we can configure the static files settings. Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”.

Configuring static files¶
1. 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 'app/example.jpg' %}" alt="My image" >

4. n addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files. For example:
STATICFILES_DIRS = [
BASE_DIR / "static",
'/var/www/static/',
]

For example, if your STATIC_URL is defined as static/, you can do this by adding the following snippet to your urls.py:

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)

👍 If you enjoyed this content, give the video a like. If you want to watch more upcoming videos, consider subscribing to the channel! →    / @skillshats  

🛝 DJANGO PLAYLIST:
   • Django  

🎬 RECOMMENDED VIDEOS:
Django Models:
→    • Add record in Django model | Python  
→    • Add record in Django model | Python  
→    • Update & Delete record in Django Mode...  

䷐ FOLLOW US:
Website: https://skillshats.com
Twitter:   / skillshats  

🏷 TAGS:
#python #django #django-models

Thanks!!
Enjoy 🤗