Django 配置静态文件

1,Debug=True 调试模式

Test/Test/settings.py

复制代码
DEBUG = True

...

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
STATIC_URL = '/static/'

1.1 创建静态文件

Test/static/6/images/Sni1.png

1.2 添加视图函数

Test/app6/views.py

复制代码
from django.shortcuts import render

# Create your views here.
def welcome(request):
    return render(request, '6/welcome.html')


def test_img(request):
    return render(request, '6/test_img.html')

1.3 添加路由地址

Test/app6/urls.py

复制代码
from django.urls import path
from . import views


urlpatterns = [
    path('welcome', views.welcome, name='welcome'),
    path('test_img', views.test_img, name='test_img'),
]

1.4 访问页面

http://127.0.0.1:8000/app6/test_img

2,Debug=False 生产模式

Test/Test/settings.py

复制代码
DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1']

....

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT=os.path.join(BASE_DIR, "media")
相关推荐
傻啦嘿哟7 小时前
某招聘平台爬虫:爬取招聘岗位数据,分析各城市薪资水平
开发语言·爬虫·python
2501_933670797 小时前
2026秋招量化分析岗技能栈:Python、SQL、统计建模、回测项目怎么准备
开发语言·python·sql
2601_962077607 小时前
python Dejavu库快速识别音频指纹实例探究
python·音乐识别·dejavu库·音频指纹识别·实例探究
科技苑7 小时前
如何用Python编程实现一个简单的Web爬虫?
人工智能·python
dayDayupbetter8 小时前
Visual C++ 2010安装与使用高手秘籍
python
隐擎fox8 小时前
深入理解网络传输层安全:TLS 指纹识别(JA3/JA4)原理与 Python 协议层检测实战
爬虫·python·网络协议·安全·网络安全·https
码事漫谈8 小时前
多人共用一个 key,缓存命中率会不会因此降低?
后端
医疗信息化王工8 小时前
DataForge:基于 Python 的数据库批量导出 Excel 工具——从架构到部署的全流程实战
数据库·python·excel
考虑考虑8 小时前
docker compose环境变量替换
运维·后端·自动化运维
小玮看世界9 小时前
[Python]从合并区间到传感器融合区:合并区间在传感器区域融合的实际落地
开发语言·python