django前端怎么设置 静态文件路径
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django前端怎么设置 静态文件路径相关的知识,希望对你有一定的参考价值。
参考技术A 设置一个路径变量APP_PATH=os.path.dirname(os.path.dirname(__file__))
#
Absolute
path
to
the
directory
static
files
should
be
collected
to.
#
Don't
put
anything
in
this
directory
yourself;
store
your
static
files
#
in
apps'
"static/"
subdirectories
and
in
STATICFILES_DIRS.
#
注意要修改STATIC_ROOT变量
STATIC_ROOT
=
os.path.join(APP_PATH,'static').replace('\\','/')
#
URL
prefix
for
static
files.
#
Example:
ample.com/static/",
"h
ic.example.com/"
STATIC_URL
=
'/static/'
#
当然还有STATICFILES_DIRS变量
STATICFILES_DIRS
=
(
#
Put
strings
here,
like
"/home/html/static"
or
"C:/www/django/static".
#
Always
use
forward
slashes,
even
on
Windows.
#
Don't
forget
to
use
absolute
paths,
not
relative
paths.
os.path.join(APP_PATH,'mobile_oa_server/static').replace('\\','/'),
)
Python Django 之 静态文件存放设置
一、静态文件存放路径设置STATICFILES_DIRS
1、在django项目目录下面新建静态文件保存目录
2、在setting中添加相应寻找静态文件目录的配置
STATICFILES_DIRS=(
os.path.join(BASE_DIR,"static"),
)
3、将jquery放入相应路径中
4、验证静态文件的路径设置
1)url
from django.contrib import admin
from django.urls import path
from blog import views
urlpatterns = [
path(\'admin/\', admin.site.urls),
#path(\'cur_time/\',views.cur_time),
path(\'userInfo/\',views.userInfo),
]
2)views
from django.shortcuts import render,HttpResponse
import datetime
from blog import models
def userInfo(req):
if req.method=="POST":
u=req.POST.get("username",None)
s=req.POST.get("sex", None)
e=req.POST.get("email", None)
models.UserInfo.objects.create(
username=u,
sex=s,
email=e,
)
user_list=models.UserInfo.objects.all()
return render(req,"index.html",{"user_list":user_list})
3)templates引入jquery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/userInfo/" method="post">
<p>姓名<input type="text" name="username"></p>
<p>性别<input type="text" name="sex"></p>
<p>邮箱<input type="text" name="email"></p>
<p><input type="submit" name="submit"></p>
</form>
<hr>
<h1>
数据展示
</h1>
<table border="1px">
<tr>
<td>姓名</td>
<td>性别</td>
<td>邮箱</td>
</tr>
<br/>
<tr>
{% for i in user_list %}
<td>{{ i.username }}</td>
<td>{{ i.sex }}</td>
<td>{{ i.email }}</td>
{% endfor %}
</tr>
<br/>
</table>
<script src="/static/jquery-1.12.4.min.js"></script>
<script>
$("h1").css("color","red")
</script>
</body>
</html>
4)重启验证
python manage.py runserver 8080
二、静态文件存放路径别名STATIC_URL
1、STATIC_URL在setting中设置
STATIC_URL = \'/static/\'
或者
STATIC_URL = \'/abc/\'
。。。。
2、STATIC_URL好处
无论后端怎么修改静态文件的保存路径,
前端依然可以使用STATIC_URL设置的别名继续使用,
不会随着后端的修改而修改。
以上是关于django前端怎么设置 静态文件路径的主要内容,如果未能解决你的问题,请参考以下文章