Django-静态文件-图片音乐视频
一般访问static路径下的图片:
1、views.py
--------------------------------------------------
from django.shortcuts import render
def test_static(request):
return render(request, 'test_static.html')
-----------------------------------------------------
2、urls.py
-----------------------------------------------------
from django.contrib import admin
from django.urls import path
from static_app.views import *
urlpatterns = [
path('admin/', admin.site.urls),
#注意这里路由的名不能和目录的名一样
path('test_static/', test_static, name='static'),
]
--------------------------------------------------------------
3、test_static.html
--------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>静态文件</title>
</head>
<body>
<!--绝对地址-->
<!--/static/image/me.jpg 就是图片的路径-->
<img src="http://127.0.0.1:8000/static/image/me.jpg" width="200px" height="200px">
<!--相对地址-->
<img src="/static/image/me.jpg" width="200px" height="200px">
</body>
</html>
-----------------------------------------------------------------------------------------------
4、setting.py
在STATIC_URL = '/static/' 下面添加
----------------------------------------------------------------
STATIC_URL = '/static/'
#这是一个元组、static为目录名
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
-----------------------------------------------------------------
Django中提供的方案--推荐
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>静态文件</title>
</head>
<body>
<!--先加载static标签-->
{% load static %}
<!--使用static标签-->
<img src="{% static 'image/me.jpg' %}" width="200px" height="200px">
</body>
</html>