
一、首先在项目根路径下新建一个static文件夹,然后在static文件夹下可以新建相应的css、imgs、js等文件夹,用于存放css、img、js等静态文件。
二、项目配置
1、打开settings.py,在底部添加:
2、打开urls.py,在 urlpatterns 中添加:(注意,如果你在blog目录下也建立了urls.py,那么就得在blog/urls.py中做修改。别忘了import settings):
(不过我经过实测,不加这一个,也可以完成静态文件的访问)
3、在html模板最上面添加:
然后在需要的地方按照这个格式进行调用,如:
最后重新运行项目,img等本地静态文件就可以被django找到了,这时模板对应的页面就可以显示使用img等文件了。
设置一个路径变量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的话,你可以把python的安装环境加到系统变量(我记得是自动添加的,可以在dos下直接运行python)至于django的话,可以直接运行django-admin startproject mysite2(django-admin不用.py),不用python django-admin.py startproject mysite2(如果要的话,需要在python安装包下的script添加到系统环境变量)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)