项目班 07 Model与数据优化

  html默认可以用直接用的方法和变量

{{ static_url(p.image_url) }} #static_url表示直接获取静态文件url

{{ handler.current_user }} #handler.current_user表示直接获取用户名

  handlers/main.py 更新

class IndexHandler(AuthBaseHandler):
"""
Home page for user,photo feeds 主页
"""
@tornado.web.authenticated #如果没有登录(拿不到current_user),会自动跳转到 /login ,并把当前url加到next参数里面
def get(self,*arg,**kwargs):
posts = get_post_for(self.current_user) #current_user 是获取get_current_user的用户名;然后拿到该用户的图片(是一个列表)
self.render('index.html',posts = posts) #在index.html里面可以直接引用posts里面的image_url,id等 class ExploreHandler(AuthBaseHandler):
"""
Explore page,photo of other users 浏览简缩图
"""
@tornado.web.authenticated
def get(self,*arg,**kwargs):
posts = get_all_posts() #获取所有图片
self.render('explore.html',posts=posts)#打开explore文件并将图片放上去

  utils/account.py 更新

def add_post_for(username,image_url,thumb_url):#将路径保存到数据库
'''
保存该用户的图片
'''
user = session.query(User).filter_by(name=username).first()#拿到user
post = Post(image_url=image_url,thumb_url=thumb_url,user=user)
session.add(post)
session.commit()
# return post.id def get_post_for(username):
'''
查看该用户图片
'''
user = session.query(User).filter_by(name=username).first() #获取用户名
# posts = session.query(Post).filter_by(user=user) #返回的是实例的对象,是一个列表list
return user.posts #获取该用户名的posts ,这个posts = session.query(Post).filter_by(user=user),
#因为在下面的Post表中relationship里面有外键约束backref='post',要查询posts的属性可以通过user.posts的方式来查找 def get_post(post_id):
'''
获取指定id的post
'''
post = session.query(Post).get(post_id) #从数据库里面查找指定id的post
return post def get_all_posts():
'''
获取所有id的post
'''
posts = session.query(Post).order_by(Post.id.desc()).limit(8) #order_by将id做下倒序排序,并且只显示最新的8个
# posts = session.query(Post).order_by(Post.id.desc())[:8] 这种方法也是可以的,前面posts取出来的是一个列表,通过[:8]的方法反序查询(切片)
return posts

  templates/index.html更新

{% extends 'base.html' %}

{% block title %}index page{% end %}

{% block content %}

<div class="row">
<div class="col-12 text-center">
<h2>关注用户最新动态</h2>
<p class="lead">最新上传的图片列表</p>
</div>
<div class="col-12 col-sm-8 offset-sm-1 align-self-center">
{% for p in posts %} {# 对posts里面的每个图片进行迭代,运行一次输出一个图片;静态路径访问:就是访问静态文件目录,通过静态路径来打开文件
后面static目录后面的目录就是服务器本地图片所存储的目录;还有就是handler访问,通过具体的handler来访问 #}
<figure class="figure" style="max-width: 500px">
<a href="/post/{{ p.id }}"> {# 跳转到main.py下面的post路由,并用正则写入posts实例p的id,通过id来找寻该图片的储存路径,进而打开目录 #}
<img src="{{ static_url(p.image_url) }}" class="figure-img img-fluid rounded" alt="a figure.">
</a>
<figcaption class="figure-caption">upload by {{ p.user.name }}</figcaption>
</figure>
{% end %}
</div> </div>
{% end %}

  templates/post.html 更新

{% extends 'base.html' %}

{% block title %}post page{% end %}

{% block content %}
<div class="row justify-content-center">
<div class="col-12 col-sm-10">
<img src="{{ static_url(post.image_url) }}" width="560px" />
<h4>{{ post.user.name }}在{{ post.created or '' }}上传</h4>
</div> </div>
{% end %}

  templates/explore.html 更新

{% extends 'base.html' %}

{% block title %}explore page{% end %}

{% block content %}
{% for p in posts%}
<a href="/post/{{ p.id }}">
<img src="{{ static_url(p.thumb_url) }}" />
</a>
{% end %}
{% end %}

项目 07 Model与数据优化的更多相关文章

  1. 项目架构开发:数据访问层之Query

    接上文 项目架构开发:数据访问层之Repository 上一章我们讲了IRepository接口,这张我们来讲IQuery 根据字面意思就可以知道,这次主要讲数据查询,上一章我们只针对单表做了查询的操 ...

  2. 项目中常用的MySQL 优化

    本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下: 一.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我 ...

  3. 《深度访谈:华为开源数据格式 CarbonData 项目,实现大数据即席查询秒级响应》

    深度访谈:华为开源数据格式 CarbonData 项目,实现大数据即席查询秒级响应   Tina 阅读数:146012016 年 7 月 13 日 19:00   华为宣布开源了 CarbonData ...

  4. 循序渐进开发WinForm项目(5)--Excel数据的导入导出操作

    随笔背景:在很多时候,很多入门不久的朋友都会问我:我是从其他语言转到C#开发的,有没有一些基础性的资料给我们学习学习呢,你的框架感觉一下太大了,希望有个循序渐进的教程或者视频来学习就好了. 其实也许我 ...

  5. ASP.NET MVC 学习3、Controller左手从Model获取数据,右手传递到View页面

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-dat ...

  6. 项目架构开发:数据访问层之Cache

    数据访问层简单介绍 数据访问层,提供整个项目的数据访问与持久化功能.在分层系统中所有有关数据访问.检索.持久化的任务,最终都将在这一层完成. 来看一个比较经典的数据访问层结构图 大概可以看出如下信息 ...

  7. 项目架构开发:数据访问层之Logger

    接上文 项目架构开发:数据访问层之Cache 本章我们继续ILogger的开发 ILogger.cs public interface ILogger { void Info(object messa ...

  8. 项目架构开发:数据访问层之Repository

    接上文 项目架构开发:数据访问层之Logger 本章我们继续IRepository开发,这个仓储与领域模式里边的仓储有区别,更像一个工具类,也就是有些园友说的“伪仓储”, 这个仓储只实现单表的CURD ...

  9. 项目架构开发:数据访问层之UnitOfWork

    接上文 项目架构开发:数据访问层之IQuery 本章我们继续IUnitOfWork的开发,从之前的IRepository接口中就可以看出,我们并没有处理单元事务, 数据CUD每次都是立即执行的,这样有 ...

随机推荐

  1. Qt Quick中的信号与槽

    在QML中,在Qt Quick中,要想妥善地处理各种事件,肯定离不开信号与槽,本博的主要内容就是整理Qt 中的信号与槽的内容. 1. 链接QML类型的已知信号 QML中已有类型定义的信号分为两类:一类 ...

  2. java枚举学习enum

    java 1.5以后才出现enum的关键字 所有的enum类都继承自Enum类,所以enum类无法再继承其他的类,可以实现接口,枚举类出了不能被继承其余的与普通类的特性一致, 枚举类的构造函数只能自己 ...

  3. 霍夫变换Hough

    http://blog.csdn.net/sudohello/article/details/51335237 霍夫变换Hough 霍夫变换(Hough)是一个非常重要的检测间断点边界形状的方法.它通 ...

  4. VC++6.0注释快捷键的添加使用

    在eclipse等编辑工具中都有注释代码的快捷键,但是vc++6.0没有. vc++是以VB为脚本来控制的,在网上找到了一个VB的脚本供大家使用.   工具/原料 VC++6.0 方法/步骤 打开VC ...

  5. Poj_1002_java解决

    一.Description 电话号码的标准格式是七位十进制数,并在第三.第四位数字之间有一个连接符.电话拨号盘提供了从字母到数字的映射,映射关系如下: A, B, 和C 映射到 2 D, E, 和F ...

  6. glusterfs安装配置简单使用

    GlusterFS是一种分布式分布式文件系统,默认采用无中心完全对等架构,搭建维护使用十分简单,是很受欢迎的分布式文件系统. 官网https://www.gluster.org/,官网上表示Glust ...

  7. tomcat安装与运行

    实验环境:CentOS7 使用系统yum仓库安装: #安装基本包和开发工具包 [root@~ localhost]#yum install -y java-1.8.0-openjdk java-1.8 ...

  8. pythoon_interview_redit

    easy/intermediate What are Python decorators and how would you use them?How would you setup many pro ...

  9. javascript数字千分分隔符

    function thousandBitSeparator(num) { num=num.toFixed(2); return num && num .toString() .repl ...

  10. SpringMVC 学习笔记(拦截器的配置))

    在设置SpringMVC的拦截器时,需要在SpringMVC中配置 拦截器对象,拦截器的的对象要 实现 HandlerInterceptor 接口 拦截器类的设置: public class inte ...