07 静态文件配置

编辑本博客

静态文件static

网站使用的js,css,img等文件

在项目中创建一个static文件夹

配置文件settings.py

新加STATIC_URL的别名,在settings.py中添加如下行

STATICFILES_DIRS = [
    os.path.join(BASE_DIR , "static")
]

html文件中静态文件引入方式

<script src="/static/jquery-3.3.1.js"></script>

 对静态文件解耦

在static文件夹下创建对应项目的目录,并将所需静态文件放在其中

在html中引入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>当前时间</title>
    <link rel="stylesheet" href="/static/app01/timer.css">
    <script src="/static/jquery-3.3.1.js"></script>
</head>
<body>
<h1>当前时间:{{ date }}</h1>
</body>
<script src="/static/app01/timer.js"></script>
<script>
</script>
</html>
View Code

静态文件media

Media配置之MEDIA_ROOT

django有两种静态文件:

/static/        前端需求资源

/media/       所有用户上传的文件都在该文件夹中

django配置media文件件配置,在setting文件中操作

一旦配置过media,那么FileField中上传的文件将放置在media中

Media配置之MEDIA_URL

使浏览器访问media下的数据

setting下添加MEDIA_URL配置

MEDIA_ROOT=os.path.join(BASE_DIR,'media')

在url中配置路由

from django.views.static import serve
from cnblog import settings
#media配置
re_path(r'media/(?P<path>.*)$',serve,{"document_root":settings.MEDIA_ROOT})

 

posted @ 2018-06-27 11:12  丫丫625202  阅读(144)  评论(0编辑  收藏  举报