Python基础-Day 11 - 编写日志创建页
2018-01-11 10:05
246 查看
一、项目来源
廖雪峰老师 Day 11 - 编写日志创建页https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143233918656129f4ad3ac29e4f728dc72b5d2368215a000
二、编码
1. 管理员登陆
记得事先先设置 admin 为 1============================================================================================== app.py @asyncio.coroutine def 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.coroutine def 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: 1 INFO:root:set current user: 123@123.com
2.写博客-UI
manage_blog_edit.html <div id="vm" class="uk-width-2-3"> <form v-on="submit: submit" class="uk-form uk-form-stacked"> <div class="uk-alert uk-alert-danger uk-hidden"></div> <div class="uk-form-row"> <label class="uk-form-label">标题:</label> <div class="uk-form-controls"> <input v-model="name" name="name" type="text" placeholder="标题" class="uk-width-1-1"> </div> </div> <div class="uk-form-row"> <label class="uk-form-label">摘要:</label> <div class="uk-form-controls"> <textarea v-model="summary" rows="4" name="summary" placeholder="摘要" class="uk-width-1-1" style="resize:none;"></textarea> </div> </div> <div class="uk-form-row"> <label class="uk-form-label">内容:</label> <div class="uk-form-controls"> <textarea v-model="content" rows="16" name="content" placeholder="内容" class="uk-width-1-1" style="resize:none;"></textarea> </div> </div> <div class="uk-form-row"> <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-save"></i> 保存</button> <a href="/manage/blogs" class="uk-button"><i class="uk-icon-times"></i> 取消</a> </div> </form> </div>
3.创建博客
============================================================== 页面的点击事件 __base__.html <li><a href="/manage_blogs_create"><i class="uk-icon-sign-in"></i>写博客</a></li> ============================================================== handles.py @get('/manage_blogs_create') def manage_create_blog(): return { '__template__': 'manage_blog_edit.html', 'id': '', 'action': '/api/blogs' } ============================================================== 跳转到编辑界面 manage_blog_edit.html <div class="uk-form-row"> <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-save"></i> 保存</button> <a href="/manage/blogs" class="uk-button"><i class="uk-icon-times"></i> 取消</a> </div> 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基础-项目实战-Day 12 - 编写日志列表页
- 在Python的web框架中编写创建日志的程序的教程
- Python3教程Web开发实战梳理-day11(编写日志创建页MVVC)
- Python版——博客网站<四> 编写日志创建页
- 在Python的web框架中编写创建日志的程序的教程
- [零基础学python]编写类之一创建实例
- python基础日志16年8月5日
- 11-Python3从入门到实战—基础之生成器和迭代器
- Python 基础-项目实战-day 13 Android客户端开发(项目完结)
- 重学Python - Day 01 - python基础 -> python版本选择+第一个小程序
- 零基础写python爬虫之使用Scrapy框架编写爬虫
- PHP基础教程-11 创建单位转换器
- python数据库sqlite基础(一)-------数据库创建,表的建立,数据录入,数据查询
- Python3 Tkinter基础 Radiobutton 创建三个单选钮 基础示例
- python基础(1):文件的读取、创建、追加、删除、清空
- Python基础===使用virtualenv创建一个新的运行环境
- Python3基础 tuple 创建空元组或者只有一个元素的元组 并 用乘法成倍扩充
- PYTHON_DAY_11_20160607
- Python3基础 dict 创建字典 空字典
- 零基础入门学习Python(11)--列表:一个打了激素的数组(2)