十六天编写一个博客应用程序(3)

额额,知道刚才,已经完成了别人项目的迁移和部署完毕了。参考大佬的项目来做的。

我是在廖雪峰的python实战教程下面的评论里面找到了这个大佬的项目,github源码。接下来就是部署的环节:

1.下载大佬的github源码

我也可以提供百度云链接,提取码f8h9。

下载解压之后,将其

内的所有文件复制到你pycharm的一个空项目中去(即该awesome_blogs原本为空,现在将上面那些文件全都复制到该空文件夹中去)

2.创建数据库的数据

将该段代码

-- schema.sql

drop database if exists awesome;
drop user if exists 'www-data'@'localhost';

create database awesome;

use awesome;

create user 'www-data'@'localhost' identified by 'www-data';
alter user 'www-data'@'localhost' identified with mysql_native_password by 'www-data';
grant select, insert, update, delete on awesome.* to 'www-data'@'localhost';

create table users (
    `id` varchar(50) not null,
    `email` varchar(50) not null,
    `passwd` varchar(50) not null,
    `admin` bool not null,
    `name` varchar(50) not null,
    `image` varchar(500) not null,
    `created_at` real not null,
    unique key `idx_email` (`email`),
    key `idx_created_at` (`created_at`),
    primary key (`id`)
) engine=innodb default charset=utf8;

create table blogs (
    `id` varchar(50) not null,
    `user_id` varchar(50) not null,
    `user_name` varchar(50) not null,
    `user_image` varchar(500) not null,
    `name` varchar(50) not null,
    `summary` varchar(200) not null,
    `content` mediumtext not null,
    `created_at` real not null,
    key `idx_created_at` (`created_at`),
    primary key (`id`)
) engine=innodb default charset=utf8;

create table comments (
    `id` varchar(50) not null,
    `blog_id` varchar(50) not null,
    `user_id` varchar(50) not null,
    `user_name` varchar(50) not null,
    `user_image` varchar(500) not null,
    `content` mediumtext not null,
    `created_at` real not null,
    key `idx_created_at` (`created_at`),
    primary key (`id`)
) engine=innodb default charset=utf8;

复制到sqlyog等执行sql语句的软件中,执行该所有语句,就能在你的mysql数据库中创建数据库和数据表

你可以在www目录下新建一个test_connected.py,他的内容为:

import logging; logging.basicConfig(level=logging.INFO)
import asyncio
from aiohttp import web

## 定义服务器响应请求的的返回为 "Awesome Website"
async def index(request):
    return web.Response(body=b'<h1>Awesome Website</h1>', content_type='text/html')

## 建立服务器应用,持续监听本地9000端口的http请求,对首页"/"进行响应
def init():
    app = web.Application()
    app.router.add_get('/', index)
    web.run_app(app, host='127.0.0.1', port=9000)

if __name__ == "__main__":
    init()

点击运行测试一下。正常结果如图:

访问http://127.0.0.1:9000即可打开下面这个页面

你可以在www目录下新建test_orn.py,他的内容为:

import orm
import asyncio
from models import User, Blog, Comment

async def test(loop):
    await orm.create_pool(loop=loop, user='root', password='root', db='awesome')
    u = User(name='Test', email='test@qq.com', passwd='1234567890', image='about:blank')
    await u.save()
    ## 网友指出添加到数据库后需要关闭连接池,否则会报错 RuntimeError: Event loop is closed
    orm.__pool.close()
    await orm.__pool.wait_closed()
if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(test(loop))
    loop.close()

运行该代码,正常结果如图:

而且你会发现你数据库的user表中会插入一条数据

3.至此,你就可以运行 app.py文件了,然后你访问http://127.0.0.1:9000就可以打开如下

然后,你就可以对该原型进行自己的设计,如果你觉得这个页面朴实无华,那你就可以把他设计得酷炫吊炸天。开始动手吧,骚年!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值