Django1.11.4 在前端显示图片

基础内容可以参考: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.其它可参考:版本不同,会有差异

1.Django显示图片。。

2.Django显示图片。。。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值