项目简介

为什么做一个模仿 Instagram 的应用

  • 偏后端和后台的开发
  • 充分利用 tornado 的特点
  • 积累项目经验,巩固知识点

Instagram 主要组成

  • 发现或最近上传的图片页面
  • 所关注的用户图片流
  • 单个图片详情页面
  • 数据库 Database
  • 用户档案 User Profile

可以附加的功能

  • 图片添加文字
  • 图片修改
  • 其他有用的小功能

怎么做

  • 从最简单开始,迭代增加功能
  • 用户,登陆,关注等
  • 数据库保存
  • UI 和 Web 界面美化
  • 外部连接

开始功能

运行基本程序

  • ​app.py​
  • handlers 配置
  • templates 和 static 目录设置

三个基本页面

  • 发现或最近上传的图片页面 /explore ExploreHandler
  • 所关注的用户图片流 / IndexHandler
  • 单个图片详情页面 /post/id PostHandler

代码

项目初始化 建立基本页面

app.py

import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options

from handlers import main

define('port', default='8000', type=int, help='Listening port')


class Application(tornado.web.Application):
def __init__(self):
handlers = [
('/', main.IndexHandler),
('/explore', main.ExploreHandler),
('/post/(?P<post_id>[0-9]+)', main.PostHandler),
]
settings = dict(
debug=True,
template_path='template',
)

super(Application, self).__init__(handlers, **settings)


application = Application()

if __name__ == '__main__':
tornado.options.parse_command_line()
application.listen(options.port)
print("Server start on port {}".format(str(options.port)))
tornado.ioloop.IOLoop.current().start()

import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options

from handlers import main

define('port', default='8000', type=int, help='Listening port')


class Application(tornado.web.Application):
def __init__(self):
handlers = [
('/', main.IndexHandler),
('/explore', main.ExploreHandler),
('/post/(?P<post_id>[0-9]+)', main.PostHandler),
]
settings = dict(
debug=True,
template_path='template',
)

super(Application, self).__init__(handlers, **settings)


application = Application()

if __name__ == '__main__':
tornado.options.parse_command_line()
application.listen(options.port)
print("Server start on port {}".format(str(options.port)))
tornado.ioloop.IOLoop.current().start()

main.py

import tornado.web


class IndexHandler(tornado.web.RequestHandler):
"""
Home page for user,photo feeds.
"""

def get(self, *args, **kwargs):
# self.write('ok...')
self.render('index.html')


class ExploreHandler(tornado.web.RequestHandler):
"""
Explore page ,photo of other users.
"""

def get(self, *args, **kwargs):
# self.write('ok...')
self.render('explore.html')


class PostHandler(tornado.web.RequestHandler):
"""
Single photo page,and maybe comments
"""

def get(self, *args, **kwargs):
self.render('post.html', post_id=kwargs['post_id'])

import tornado.web


class IndexHandler(tornado.web.RequestHandler):
"""
Home page for user,photo feeds.
"""

def get(self, *args, **kwargs):
# self.write('ok...')
self.render('index.html')


class ExploreHandler(tornado.web.RequestHandler):
"""
Explore page ,photo of other users.
"""

def get(self, *args, **kwargs):
# self.write('ok...')
self.render('explore.html')


class PostHandler(tornado.web.RequestHandler):
"""
Single photo page,and maybe comments
"""

def get(self, *args, **kwargs):
self.render('post.html', post_id=kwargs['post_id'])

base.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}Title{% end %}</title>
</head>
<body>
{% block content %}
base content
{% end%}


</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}Title{% end %}</title>
</head>
<body>
{% block content %}
base content
{% end%}


</body>
</html>

index.html

{% extends 'base.html' %}

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

{% block content %}
index content
{% end %}

{% extends 'base.html' %}

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

{% block content %}
index content
{% end %}

explore.html

{% extends 'base.html' %}

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

{% block content %}
explore content
{% end %}

{% extends 'base.html' %}

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

{% block content %}
explore content
{% end %}

post.html

{% extends 'base.html' %}

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

{% block content %}
post of {{ post_id }} content
{% end %}

{% extends 'base.html' %}

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

{% block content %}
post of {{ post_id }} content
{% end %}

增加简单的图片显示

app.py

import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options

from handlers import main

define('port', default='8000', type=int, help='Listening port')


class Application(tornado.web.Application):
def __init__(self):
handlers = [
('/', main.IndexHandler),
('/explore', main.ExploreHandler),
('/post/(?P<post_id>[0-9]+)', main.PostHandler),
]
settings = dict(
debug=True,
template_path='template',
static_path='static',
)

super(Application, self).__init__(handlers, **settings)


application = Application()

if __name__ == '__main__':
tornado.options.parse_command_line()
application.listen(options.port)
print("Server start on port {}".format(str(options.port)))
tornado.ioloop.IOLoop.current().start()

import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options

from handlers import main

define('port', default='8000', type=int, help='Listening port')


class Application(tornado.web.Application):
def __init__(self):
handlers = [
('/', main.IndexHandler),
('/explore', main.ExploreHandler),
('/post/(?P<post_id>[0-9]+)', main.PostHandler),
]
settings = dict(
debug=True,
template_path='template',
static_path='static',
)

super(Application, self).__init__(handlers, **settings)


application = Application()

if __name__ == '__main__':
tornado.options.parse_command_line()
application.listen(options.port)
print("Server start on port {}".format(str(options.port)))
tornado.ioloop.IOLoop.current().start()

main.py

import tornado.web


class IndexHandler(tornado.web.RequestHandler):
"""
Home page for user,photo feeds.
"""

def get(self, *args, **kwargs):
self.render('index.html')


class ExploreHandler(tornado.web.RequestHandler):
"""
Explore page ,photo of other users.
"""

def get(self, *args, **kwargs):
self.render('explore.html')


class PostHandler(tornado.web.RequestHandler):
"""
Single photo page,and maybe comments
"""

def get(self, *args, **kwargs):
self.render('post.html', post_id=kwargs['post_id'])

import tornado.web


class IndexHandler(tornado.web.RequestHandler):
"""
Home page for user,photo feeds.
"""

def get(self, *args, **kwargs):
self.render('index.html')


class ExploreHandler(tornado.web.RequestHandler):
"""
Explore page ,photo of other users.
"""

def get(self, *args, **kwargs):
self.render('explore.html')


class PostHandler(tornado.web.RequestHandler):
"""
Single photo page,and maybe comments
"""

def get(self, *args, **kwargs):
self.render('post.html', post_id=kwargs['post_id'])

base.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}Title{% end %}</title>
</head>
<body>
{% block content %}
base content
{% end%}


</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}Title{% end %}</title>
</head>
<body>
{% block content %}
base content
{% end%}


</body>
</html>

index.html

{% extends 'base.html' %}

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

{% block content %}
{% for i in range(1,5) %}
<img src="{{ static_url("images/{}.jpg".format(i)) }} " width="666">
{% end %}
{% end %}

{% extends 'base.html' %}

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

{% block content %}
{% for i in range(1,5) %}
<img src="{{ static_url("images/{}.jpg".format(i)) }} " width="666">
{% end %}
{% end %}

explore.html

{% extends 'base.html' %}

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

{% block content %}
{% for i in range(1,5) %}
<img src="{{ static_url("images/{}.jpg".format(i)) }} " width="333">
{% end %}
{% end %}

{% extends 'base.html' %}

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

{% block content %}
{% for i in range(1,5) %}
<img src="{{ static_url("images/{}.jpg".format(i)) }} " width="333">
{% end %}
{% end %}

post.html

{% extends 'base.html' %}

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

{% block content %}
<img src="{{ static_url("images/{}.jpg".format(post_id)) }} " width=666">
{% end %}

{% extends 'base.html' %}

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

{% block content %}
<img src="{{ static_url("images/{}.jpg".format(post_id)) }} " width=666">
{% end %}


Git 使用

使用参考文档

git 使用简易指南 
http://www.bootcss.com/p/git-guide/


在Pycharm中使用GitHub - 刘江liujiangblog.com - 博客园 javascript:void(0)


Git - 起步
https://git-scm.com/book/zh/v1/%E8%B5%B7%E6%AD%A5

初次运行配置用户信息(在 cmd 里运行)

​git config --global user.name "John Doe"​

​git config --global user.email johndoe@example.com​

Linux 安装

​sudo apt-get install git​

git init  创建新仓库 
git status 查看状态
git add * 添加 (把改动添加到缓存区)
git status
git commit -m 'init commit' 提交 (改动已经提交到了 HEAD,但是还没到你的远端仓库)
git status
git config --global user.name "wushanshan"
git config --global user.email 630622194@qq.com



如果修改了 app.py 文件
git add app.py
git status
git commit -m 'modify app.py'
git diff 查看改变
git checkout -- . 撤销更改

如果修改了两个文件 一起提交
git add *
git status
git commit -m 'modify two files'

下载地址

作业

基本的三个页面

额外:Git 使用