Django Static Files Setup Guide
Setting up static files in Django can be done by following these steps:
- Set the path for static files in the settings.py file.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
- Set the URL for static files in the urls.py file of the project.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# 其他url配置
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
- Referencing static files in the template file:
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<script src="{% static 'js/script.js' %}"></script>
- You can organize static files by placing them in the static folder and creating subfolders as needed.
After setting the static files through the above steps, Django will load the static files according to the configuration and correctly reference them in the template.