博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python基础-Day 11 - 编写日志创建页
阅读量:4147 次
发布时间:2019-05-25

本文共 3745 字,大约阅读时间需要 12 分钟。

一、项目来源

廖雪峰老师 Day 11 - 编写日志创建页

二、编码

1. 管理员登陆

记得事先先设置 admin 为 1

这里写图片描述

==============================================================================================app.py@asyncio.coroutinedef auth_factory(app, handler):    @asyncio.coroutine    def auth(request):        logging.info('check user: %s %s' % (request.method, request.path))        request.__user__ = None        cookie_str = request.cookies.get(COOKIE_NAME)        if cookie_str:            user = yield from cookie2user(cookie_str)            if user:                logging.info('set current user: %s' % user.email)                request.__user__ = user        if request.path.startswith('/manage/') and (request.__user__ is None or not request.__user__.admin):            return web.HTTPFound('/signin')        return (yield from handler(request))    return auth@asyncio.coroutinedef init(loop):    yield from orm.create_pool(loop=loop, host='127.0.0.1', port=3306, user='root', password='', db='sufadi')    app = web.Application(loop=loop, middlewares=[        logger_factory, auth_factory, response_factory    ])==============================================================================================

登录详情

INFO:root:Request: GET /INFO:root:check user: GET /INFO:root:SQL: select `id`, `email`, `passwd`, `admin`, `name`, `image`, `created_at` from `users` where `id`=?INFO:root:rows returned: 1INFO:root:set current user: 123@123.com

这里写图片描述

2.写博客-UI

这里写图片描述

manage_blog_edit.html    
取消

3.创建博客

==============================================================页面的点击事件__base__.html
  • 写博客
  • ==============================================================handles.py@get('/manage_blogs_create')def manage_create_blog(): return { '__template__': 'manage_blog_edit.html', 'id': '', 'action': '/api/blogs' }==============================================================跳转到编辑界面manage_blog_edit.html
    取消
    function initVM(blog) { var vm = new Vue({ el: '#vm', data: blog, methods: { submit: function (event) { event.preventDefault(); var $form = $('#vm').find('form'); $form.postJSON(action, this.$data, function (err, r) { if (err) { $form.showFormError(err); } else { return location.assign('/api/blogs/' + r.id); } }); } } }); $('#vm').show();}==============================================================数据库的保持事件handles.py@post('/api/blogs')def api_create_blog(request, *, name, summary, content): check_admin(request) if not name or not name.strip(): raise APIValueError('name', 'name cannot be empty.') if not summary or not summary.strip(): raise APIValueError('summary', 'summary cannot be empty.') if not content or not content.strip(): raise APIValueError('content', 'content cannot be empty.') blog = Blog(user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, name=name.strip(), summary=summary.strip(), content=content.strip()) yield from blog.save() return blog

    运行保存

    这里写图片描述

    数据库

    这里写图片描述

    你可能感兴趣的文章
    【Python】如何对xml文件进行新增、修改和删除等操作
    查看>>
    【Python基础1】变量和字符串定义
    查看>>
    【Python基础2】python字符串方法及格式设置
    查看>>
    【Python】random生成随机数
    查看>>
    【Python基础3】数字类型与常用运算
    查看>>
    Jenkins迁移jobs
    查看>>
    【Python基础4】for循环、while循环与if分支
    查看>>
    【Python基础5】列表和元组
    查看>>
    【Python基础6】格式化字符串
    查看>>
    【Python基础7】字典
    查看>>
    【Python基础8】函数参数
    查看>>
    【Python基础9】浅谈深浅拷贝及变量赋值
    查看>>
    Jenkins定制一个具有筛选功能的列表视图
    查看>>
    【Python基础10】探索模块
    查看>>
    【Python】将txt文件转换为html
    查看>>
    [Linux]Shell脚本实现按照模块信息拆分文件内容
    查看>>
    Windows安装git
    查看>>
    【Python】对象属性操作内置函数(hasattr/getattr...)
    查看>>
    Python classmethod类方法修饰符
    查看>>
    Python staticmethod静态方法
    查看>>