Static File Handling in Django Applications
In daily development, we always turn on Django's Debug mode for easier debugging. In this mode, Django's built-in web server serves static files, but some configuration is needed to access them correctly. Configure settings # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.0/howto/static-files/ ST
目录4 个章节
In daily development, we always turn on Django's Debug mode for easier debugging. In this mode, Django's built-in web server serves static files, but some configuration is needed to access them correctly.
Configure settings
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_collected')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
MEDIA_URL = '/media/'
Configure routing
from django.conf import settings
from django.urls import path, include
from django.conf.urls.static import static
urlpatterns = [
...
]
# 调试模式下的静态文件服务
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This way you can access static files normally in Debug mode. However, after we deploy the application to production, Debug mode must not be enabled for security reasons. At that point you need to use a server such as nginx or Apache to serve static files.
Before that, you also need to run
python manage.py collectstatic
to collect static files from the various packages into the STATIC_ROOT we defined earlier, and then hand this path over to nginx~
nginx configuration
# Django media
location /media/ {
alias /path/to/django/static/media/; # your Django project's media files - amend as required
}
location /static/ {
alias /path/to/django/static_collected/; # your Django project's static files - amend as required
}
Also disable Django's Debug mode, so that all static files will be served by nginx~
Welcome to discuss
I've compiled a series of technical articles and materials. Reply with linux, flutter, c#, netcore, android, kotlin, java, python, and so on in the backend of the WeChat official account 「程序设计实验室」 to get related technical articles and materials. If you have any questions, you can also leave a message in the official account backend~
_EOF_
Static File Handling in Django Applications
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Django解决跨域请求问题
几种方法 使用django-cors-headers全局控制**(推荐)** 使用JsonP,只能用于Get方法 在views.py里设置响应头,只能控制单个接口 django-cors-headers 首先安装 pip install django-cors-headers 然后在settings.py里配置一番就可以 INSTALLED_APPS = [ ... 'corshe
2022-08-26 · 0_Django基础知识 · 约 1 分钟
Django使用django-filer上传图片
前言 (好像不知道写啥) 注意 django-filer目前只支持到django 1.11 setting.py INSTALLED_APPS = [ # image support 'easy_thumbnails', 'filer', 'mptt', ] # Image Upload Support THUMBNAIL_HIGH_RESOLUTION = Tr
2022-05-27 · 0_Django基础知识 · 约 1 分钟
Django清空所有数据和重建表结构
清空所有数据 python manage.py flush 重建表结构 删除数据库中所有表 删除应用文件夹下的migrations目录内所有文件,除了__init__.py 执行以下命令 python manage.py makemigrations python manage.py migrate
2021-12-18 · 0_Django基础知识 · 约 1 分钟
米家智能插座3简单开箱
米家智能插座3?不过是换个壳的旧设计。实时功率和用电量?这点功能也就勉强及格吧。自动控制电热蚊香液?真是被逼无奈的创意。
2025-10-28 · 其他 · 约 1 分钟
讨论
共 0 条加载评论中…