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")
相关推荐
巫山老妖2 小时前
从零开发一个掘金自动发布 Skill,并上架 Clawhub
后端
曲幽2 小时前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
颜酱2 小时前
图的数据结构:从「多叉树」到存储与遍历
javascript·后端·算法
雨中飘荡的记忆3 小时前
零拷贝技术深度解析
后端
uzong3 小时前
十年老员工的项目管理实战心得:有道有术
后端
Victor3565 小时前
MongoDB(31)索引对查询性能有何影响?
后端
Victor3565 小时前
MongoDB(30)如何删除索引?
后端
lizhongxuan5 小时前
多 Agent 协同机制对比
后端
IT_陈寒6 小时前
SpringBoot项目启动慢?5个技巧让你的应用秒级响应!
前端·人工智能·后端
树上有只程序猿6 小时前
2026低代码选型指南,主流低代码开发平台排名出炉
前端·后端