基础内容可以参考:python3.6.1 django1.11.4 初探
1. 结果页面:
2. 目录结构:
主要是新建static文件夹用来存放图片。具体位置在/static/images/目录下
目录结构:wordcloud是app名称
├─spidershow
│ └─__pycache__
├─static
│ └─images
├─wordcloud
│ ├─templates
│ └─__pycache__
└─__pycache__
3. 配置文件
settings.py 中添加或修改
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),
xxx.html
{% load staticfiles %}
<p><img src="{% static "images/并读新闻.png" %}"></p>
过滤器(staticfiles ),和使用的关键字不一样(static)
而且具体的路径前不可以有空格否则报错
views.py
def test(requests):
template = loader.get_template('xxx.html')
context = {}
return HttpResponse(template.render(context))#context可省略,此处主要是用来加载html模板
4.其它可参考:版本不同,会有差异
5.报错
1.You’re using the staticfiles app
"You're using the staticfiles app "
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the required STATIC_URL setting.
解决过程:
django 1.3->1.4.6 staticfiles说了一堆。不太想看。。。
最后新建工程才发现是settings里 误删了STATIC_URL = ‘/static/’
2.Unknown column ‘sc_20170816.id
.django.db.utils.InternalError: (1054, "Unknown column 'sc_20170816.id' in 'field list'")
不报错有几个必要条件:
1).数据库的表有主键
2).数据库的表和django模型对得上号。
对应解决方式
1):取数据库把主键添上
2):重新生成models文件 (在命令行 工程目录下执行python manage.py inspectdb > models.py)会在工程(project)目录下生成对应数据库的模型文件,需要拷贝到app里面替换原有的models
可参考:django1.11.4 从mysql中同步已存在的表,自动创建models