官方文档:
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 'my_app/example.jpg' %}" alt="My image">
4 . Store your static files in a folder called static in your app. For example my_app/static/my_app/example.jpg.
重点是这里,默认后台管理的static
在 DEBUG=True 的时候,可以正常使用,
在 DEBUG=False 的时候,就访问不到了。
这个时候就需要这个操作,
# 设置静态文件目录
STATIC_ROOT = "/var/www/example.com/static/"
# 集合静态文件到 STATIC_ROOT
python manage.py collectstatic
参考:
https://docs.djangoproject.com/en/4.0/howto/static-files/