django中static文件的处理
高洛峰
高洛峰 2016-11-14 10:08:32
0
1
1192

先帖代码跟项目结构项目结构

├── gameOps
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── dufgame
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── models.py
│   ├── signals.py
│   ├── templates
│   │   └── dufindex.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── run.py
├── static
│   ├── css
│   │   ├── base.css
│   │   ├── bootstrap.css
│   │   ├── bootstrap.css.map
│   │   ├── bootstrap.min.css
│   │   ├── bootstrap-theme.css
│   │   ├── bootstrap-theme.css.map
│   │   └── bootstrap-theme.min.css
│   ├── fonts
│   │   ├── glyphicons-halflings-regular.eot
│   │   ├── glyphicons-halflings-regular.svg
│   │   ├── glyphicons-halflings-regular.ttf
│   │   └── glyphicons-halflings-regular.woff
│   └── js
│       ├── bootstrap.js
│       ├── bootstrap.min.js
│       ├── jquery.js
│       ├── jquery.min.js
│       └── npm.js
└── templates
    ├── base.html
    ├── leftpage.html
    └── tmp.html

templates/base.html 为基础模板
访问:http://192.168.50.137:8000/呈现的是base.html内容
dufgame/templates/dufindex.html中的内容部分继承了templates/base.html中的内容
访问:http://192.168.50.137:8000/duf/呈现的是dufgame/templates/dufindex.html中的内容

gameOps/setting.py中关于静态文件的设置

STATIC_URL = '/static/'
STATICFILES_DIRS  = (os.path.join(BASE_DIR,'static'),)

templates/base.html中的静态文件内容

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Game Ops</title>

{% load staticfiles %}
<link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<link href="{% static "css/base.css" %}" rel="stylesheet">

访问:http://192.168.50.137:8000/duf/打印的日志信息

INFO "GET /duf/ HTTP/1.1" 200 8634
INFO "GET /static/css/base.css HTTP/1.1" 304 0
WARNING "GET /duf/static/js/jquery.min.js HTTP/1.1" 404 2789
WARNING "GET /duf/static/js/bootstrap.min.js HTTP/1.1" 404 2798
WARNING "GET /duf/static/js/jquery.min.js HTTP/1.1" 404 2789
WARNING "GET /duf/static/js/bootstrap.min.js HTTP/1.1" 404 2798

问题来了,我的静态文件要怎么设置才能在访问http://192.168.50.137:8000/duf/时候也能调用到。

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
三叔

可以参考这个来定义static文件https://segmentfault.com/a/1190000007132294


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!