越发觉得这是一个细心活,文件变多以后一定要学会从异常的Traceback信息里向上排错。
现在是时候把模型(model)-视图(view)-控制器(controller)联调一下了。
先写一个URL处理文件handlers.py:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re, time, json, logging, hashlib, base64, asyncio
from coroweb import get, post
from models import User, Comment, Blog, next_id
# 参数aiohttp.web.request实例,包含了所有浏览器发送过来的HTTP协议里面的信息
@get('/')
async def index(request):
users = await User.findAll()
return {'__template__': 'test.html', 'users': users}
然后是模板文件test.html,在templates文件夹下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Test users - Awesome Python Webapp</title>
</head>
<body>
<h1>All users</h1>
{% for u in users %}
<p>{{ u.name }} / {{ u.email }}</p>
{% endfor %}
</body>
</html>
确保users表里有条目,然后我们来试一下效果:
$ sudo /etc/init.d/mysql start
$ python3 app.py
INFO:root:Found model: User (table: users)
INFO:root: Found mapping: id ==> <IntegerField, bigint:None>
INFO:root: Found mapping: name ==> <StringField, varchar(100):None>
INFO:root:Create database connection pool...
INFO:root:Init jinja2...
INFO:root:Set jinja2 template path: /home/myweb/py3-webapp/www/templates
INFO:root:Found model: User (table: users)
INFO:root: Found mapping: admin ==> <BooleanField, boolean:None>
INFO:root: Found mapping: email ==> <StringField, varchar(50):None>
INFO:root: Found mapping: id ==> <StringField, varchar(50):None>
INFO:root: Found mapping: name ==> <StringField, varchar(50):None>
INFO:root: Found mapping: created_at ==> <FloatField, real:None>
INFO:root: Found mapping: image ==> <StringField, varchar(500):None>
INFO:root: Found mapping: passwd ==> <StringField, varchar(50):None>
INFO:root:Found model: Blog (table: blogs)
INFO:root: Found mapping: created_at ==> <FloatField, real:None>
INFO:root: Found mapping: id ==> <StringField, varchat(50):None>
INFO:root: Found mapping: name ==> <StringField, varchar(50):None>
INFO:root: Found mapping: user_name ==> <StringField, varchar(50):None>
INFO:root: Found mapping: summary ==> <StringField, varchar(200):None>
INFO:root: Found mapping: content ==> <TextField, text:None>
INFO:root: Found mapping: user_id ==> <StringField, varchar(50):None>
INFO:root: Found mapping: user_image ==> <StringField, varchar(500):None>
INFO:root:Found model: Comment (table: comments)
INFO:root: Found mapping: created_at ==> <FloatField, real:None>
INFO:root: Found mapping: id ==> <StringField, varchar(50):None>
INFO:root: Found mapping: content ==> <TextField, text:None>
INFO:root: Found mapping: user_name ==> <StringField, varchar(50):None>
INFO:root: Found mapping: user_image ==> <StringField, varchar(500):None>
INFO:root: Found mapping: user_id ==> <StringField, varchar(50):None>
INFO:root: Found mapping: blog_id ==> <StringField, varchar(50):None>
INFO:root:add route GET / => index(request)
INFO:root:add static /static/ => /home/myweb/py3-webapp/www/static
INFO:root:Server started at http://192.168.179.140:7000...
INFO:root:Request: GET /
INFO:root:Response handler...
INFO:root:Call with arg: {'request': <Request GET / >}
INFO:root:SQL: select `id`, `admin`, `email`, `name`, `created_at`, `image`, `passwd` from `users`
INFO:root:Rows returned: 3
INFO:aiohttp.access:192.168.179.1 - - [14/Nov/2017:07:06:30 +0000] "GET / HTTP/1.1" 200 394 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
INFO:root:Request: GET /favicon.ico
INFO:root:Response handler...
INFO:aiohttp.access:192.168.179.1 - - [14/Nov/2017:07:06:30 +0000] "GET /favicon.ico HTTP/1.1" 404 172 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
打开浏览器:

不过这里还有一个问题,因为需要调试、改bug,较频繁地运行app.py会报如下错:
OSError: [Errno 98] error while attempting to bind on address ('192.168.179.140', 8000): address already in use
create_server()的reuse_address和reuse_port参数设成True也无济于事,只能手动改端口...
网友评论