python---tornado初识(2)实现登录和发布文章
# coding:utf8
# __author: Administrator
# date: 2018/3/6 0006
# /usr/bin/env python import tornado.ioloop
import tornado.web
import uimethod as mt
import uimodule as md name_list=[] #用户信息
USER_INFO={'login_status':None,'name':None}
#文章列表
NEWS_LIST=[
{"title":"dfwafaw","content":"这都是字母"}
] #原来测试模板
class MainHandler(tornado.web.RequestHandler):
def get(self):
# self.write("Hello World")
# 默认当前路径寻找
# print(self.get_argument('name'))
# print(self.get_argument('age'))
self.render("s1.html",ccc=name_list,nm="mmp") def post(self, *args, **kwargs):
name=self.get_argument('xxx')
name_list.append(name)
self.render("s1.html",ccc=name_list) #显示首页
class HomeHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html',user_status=USER_INFO['login_status'],user_name=USER_INFO['name'],content=NEWS_LIST)
#传参也可以是字典,在模板按字典使用 #处理登录和退出
class LoginHandler(tornado.web.RequestHandler):
def post(self, *args, **kwargs):
name=self.get_argument('username',None)
pawd=self.get_argument('password',None)#无法获取时候设为None,防止出错
if name == "ld" and pawd == '':
USER_INFO['login_status']=True
USER_INFO['name']=name
self.redirect('/home') def get(self, *args, **kwargs):
status=self.get_argument('quit')
if status == 'true':
USER_INFO['login_status']=False
USER_INFO['name']=None
self.redirect('/home') #文章发布
class AddNewsHandler(tornado.web.RequestHandler):
def post(self, *args, **kwargs):
title=self.get_argument('title',None)
content=self.get_argument('content',None)
if title and USER_INFO['login_status']:
NEWS_LIST.append({"title":title,"content":content})
#做一个跳转
self.redirect('home')#是跳转方法,不是页面 st ={
"template_path": "template",#模板路径配置
"static_path":'static', #js css等静态文件路径配置 无论这里配置了什么路径,在静态文件中使用都是用static
"static_url_path":'/ss/', #在static_path必须存在的基础上 类似于对其取了一个别名
#若是没有static_url_prefix,则在静态文件中的资源获取为static/s1.css
#当存在static_url_prefix时,(前提已经存在static_path),这时具体路径程序已经获取,你只需要在资源前面加上这个前缀,不需要自己去写具体url
#就是可以看做为static_path起了一个别名
#static_url_prefix了解即可,不常用
'ui_methods':mt, #自定义函数在模板中使用{{}}
'ui_modules':md, #自定义类在模板中使用{% %}
} #路由映射 匹配执行,否则404
application = tornado.web.Application([
(r"/index",MainHandler),
(r"/home",HomeHandler),
(r"/login",LoginHandler),
(r"/addNews",AddNewsHandler),
],**st) if __name__=="__main__":
application.listen(8080) #io多路复用
tornado.ioloop.IOLoop.instance().start()
模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--<link rel="stylesheet" href="static/plugins/bootstrap3/css/bootstrap.css">-->
<!--<script src="static/js/jquery.js"></script>-->
<!--<script src="static/plugins/bootstrap3/js/bootstrap.js"></script>--> <!--注意要想在模板文件中使用static_url必须定义static_url_prex或者static_url_path,两者类似-->
<link rel="stylesheet" href="{{static_url('plugins/bootstrap3/css/bootstrap.css')}}}">
<link rel="stylesheet" href="{{static_url('css/index.css')}}">
<script src='{{static_url("js/jquery.js")}}'></script >
<script src="{{static_url('plugins/bootstrap3/js/bootstrap.js')}}"></script> </head>
<body>
<div>
{% if user_status %}
<h1>你好:{{user_name}}<a onclick="PostNews();">发布消息</a><div style="float: right;"><a href="/login?quit=true">退出</a></div></h1> {% else %}
<h1>请先<a onclick="Login();">登录</a></h1>
{% end %}
</div>
<div class="content-list">
{% for item in content %}
<div class="item">
<div class="title">{{item['title']}}</div>
<div class="content">{{item['content']}}</div>
</div>
{% end %}
</div>
<div class="modal fade" id="login" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="login" method="post">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">用户登录</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label class="control-label">用户名:</label>
<input type="text" class="form-control" name="username">
</div>
<div class="form-group">
<label class="control-label">密码:</label>
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div> <div class="modal fade" id="pub" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="addNews" method="post">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">用户登录</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label class="control-label">标题:</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label class="control-label">内容:</label>
<textarea class="form-control" name="content" id="message-text"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
<script>
function Login(){
$("#login").modal('show');
} function PostNews(){
$("#pub").modal('show');
}
</script>
python---tornado初识(2)实现登录和发布文章的更多相关文章
- tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片
本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tornado的后端和ajax的 ...
- python tornado websocket 多聊天室(返回消息给部分连接者)
python tornado 构建多个聊天室, 多个聊天室之间相互独立, 实现服务器端将消息返回给相应的部分客户端! chatHome.py // 服务器端, 渲染主页 --> 聊天室建立web ...
- Python Tornado简介
简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...
- Python + Selenium 自动发布文章(一):开源中国
https://blog.csdn.net/qq_28804275/article/details/80891949 https://blog.csdn.net/qq_28804275/article ...
- python爬虫scrapy框架——人工识别登录知乎倒立文字验证码和数字英文验证码(2)
操作环境:python3 在上一文中python爬虫scrapy框架--人工识别知乎登录知乎倒立文字验证码和数字英文验证码(1)我们已经介绍了用Requests库来登录知乎,本文如果看不懂可以先看之前 ...
- Python.tornado.0
Tornado https://github.com/facebook/tornado http://www.tornadoweb.org/en/stable/guide/intro.html (A ...
- Python笔记初识
Python笔记初识
- 利用python 下paramiko模块无密码登录
利用python 下paramiko模块无密码登录 上次我个大家介绍了利用paramiko这个模块,可以模拟ssh登陆远程服务器,并且可以返回执行的命令结果,这次给大家介绍下如何利用已经建立的密钥 ...
- 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块
孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...
随机推荐
- git 的安装及使用
一.Git的安装和使用 1.1 Linux下版本库的创建 1.1.1 创建一个版本库 repository,在一个合适的地方创建一个空目录: root@zengyue:/# mkdir -p /hom ...
- [转帖]VBS 教程
VBS教程 http://www.cnblogs.com/veggiegfei/p/5943260.html 原作者真牛B 网上找了好多 没找到 没想到整理的这么好, 转来学习一下 改天打印出来. V ...
- Windows 下类似于 grep 查找字符串的命令 [转帖]
https://www.cnblogs.com/zxy1992/p/4372717.html findstr的命令参数及其意义如下所示 在文件中寻找字符串. FINDSTR [/B] [/E] [/L ...
- php的一些算法题
1.有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,请编程输出两年内每个月的兔子总数为多少? <?php function getRes ...
- matplotlib绘图
fig = plt.figure() ax=plt.gca() timeList = np.array(timeList) timeList=timeList*100 timeList1 = np.a ...
- php技术–php中感叹号!和双感叹号!!的用法(三元运算)
---恢复内容开始--- if(文章==有用){狂点我;} 在php持术或其他语言中我们经常会看到感叹号的用法,有一定程序语言基础的朋友都知道单个感叹号的作用是取反的意思,也就是取当前结果的反面,如: ...
- zabbix2.2 - /tmp/FromDualMySQLagent.lock already exists
最近升级了线上的zabbix server版本,升级成功后发现日志中一直报出history和history-uint表的主键冲突数据插入不成功的信息,根据主键冲突发生的itemid去库里查,如下 my ...
- 使用正则真正的修改TP5的config.php文件
来源 https://www.kancloud.cn/manual/thinkphp5/118026 问题 前台传值后台使用Config::set()方法写入config.php文件,但是并没有真正的 ...
- Popular Cows POJ - 2186(强连通分量)
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...
- Leading and Trailing LightOJ - 1282 (取数的前三位和后三位)
题意: 求n的k次方的前三位 和 后三位 ...刚开始用 Java的大数写的...果然超时... 好吧 这题用快速幂取模求后三位 然后用一个技巧求前三位 ...orz... 任何一个数n均可以表示 ...