模板

修改index.py

#!/usr/bin/env Python
# coding=utf-8 import tornado.web
import methods.readdb as mrd class IndexHandler(tornado.web.RequestHandler):
def get(self):
usernames = mrd.select_columns(table="users",column="username")
one_user = usernames[0][0]
self.render("index.html", user=one_user) def post(self):
username = self.get_argument("username")
password = self.get_argument("password")
user_infos = mrd.select_table(table="users",column="*",condition="username",value=username)
if user_infos:
db_pwd = user_infos[0][2]
if db_pwd == password:
self.write("welcome you: " + username)
else:
self.write("your password was not right.")
else:
self.write("There is no thi user.")

readdb.py 添加select_columns 方法

def select_columns(table, column ):
sql = "select " + column + " from " + table
cur.execute(sql)
lines = cur.fetchall()
return lines

修改index.html文件

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Learning Python</title>
</head> <body>
<h2> 登录页面</h2>
<p>用用户名为:{{user}}登录</p>
<form method="POST">
<p><span>UserName:</span><input type="text" id="username"/></p>
<p><span>Password:</span><input type="password" id="password" /></p>
<p><input type="BUTTON" value="登录" id="login" /></p>
</form>
<script src="{{static_url('js/jquery-3.2.1.min.js')}}"></script>
<script src="{{static_url('js/script.js')}}"></script>
</body>

要求用户正确登录之后,跳转到另外一个页面,并且在那个页面中显示出用户的完整信息。

先修改 url.py 文件,在其中增加一些内容

#!/usr/bin/env Python
# coding=utf-8
"""
the url structure of website
""" #!/usr/bin/env Python
# coding=utf-8
"""
the url structure of website
""" from handlers.index import IndexHandler
from handlers.user import UserHandler url = [
(r'/', IndexHandler),
(r'/user', UserHandler),
]

然后就建立 handlers/user.py 文件

#!/usr/bin/env Python
# coding=utf-8 import tornado.web
import methods.readdb as mrd class UserHandler(tornado.web.RequestHandler):
def get(self):
username = self.get_argument("user")
user_infos = mrd.select_table(table="users",column="*",condition="username",value=username)
self.render("user.html", users = user_infos)

修改script.js

/**
* Created by fulong on 2017/6/14.
*/
$(document).ready(function(){
$("#login").click(function(){
var user = $("#username").val();
var pwd = $("#password").val();
var pd = {"username":user, "password":pwd};
$.ajax({
type:"post",
url:"/",
data:pd,
cache:false,
success:function(data){
window.location.href = "/user?user="+data;
},
error:function(){
alert("error!");
},
});
});
});

user.html 模板

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Learning Python</title>
</head>
<body>
<h2>Your informations are:</h2>
<ul>
{% for one in users %}
<li>username:{{one[1]}}</li>
<li>password:{{one[2]}}</li>
<li>email:{{one[3]}}</li>
{% end %}
</ul>
</body>

模版中

    {% set website = "<a href='http://www.itdiffer.com'>welcome to my website</a>" %}
{{ website }}

需要更改成如下方式完成 不转义,因为模版会自动帮转义

{% raw website %}

模版中备查函数

  • escape(s):替换字符串 s 中的 &、<、> 为他们对应的 HTML 字符。
  • url_escape(s):使用 urllib.quote_plus 替换字符串 s 中的字符为 URL 编码形式。
  • json_encode(val):将 val 编码成 JSON 格式。
  • squeeze(s):过滤字符串 s,把连续的多个空白字符替换成一个空格。

cookies

生成安全的密钥

>>> import base64, uuid
>>> base64.b64encode(uuid.uuid4().bytes)
'w8yZud+kRHiP9uABEXaQiA=='

如果要获取此 cookie,用 self.get_secure_cookie(username) 即可。

实例:https://emptysqua.re/blog/refactoring-tornado-coroutines/

最新文章

  1. 实现css两端对齐
  2. js中Unicode转义序列
  3. IoC组件Unity再续~根据类型字符串动态生产对象
  4. DB2环境设置
  5. 2016.6.21 PHP与MqSQL交互之图片读取
  6. focus 、blur和focusin,focusout的区别
  7. hdu - 1240 Nightmare &amp;&amp; hdu - 1253 胜利大逃亡(bfs)
  8. Struts中常用的几个技术
  9. Swift学习之构造方法
  10. python实战第一天-paramiko模块并练习
  11. Angular 基本内置服务和筛选器
  12. nginx ------反向代理和负载均衡
  13. os模块 与 sys模块
  14. Cracking The Coding Interview 9.0
  15. template.helper 多参数
  16. 【进阶修炼】&mdash;&mdash;改善C#程序质量(9)
  17. [笔记]linux命令学习
  18. C++STL 中的容器整体/逐元素操作方法 少写80%for循环
  19. 学习C#基础知识这段时间
  20. 1001. 温度转换 (Standard IO)

热门文章

  1. Neo4J图库的基础介绍(一)
  2. WPF自学入门(四)WPF路由事件之自定义路由事件
  3. Asp.net mvc 5 razor
  4. mybatis快速入门(八)-spring-mybatis动态代理整合
  5. 蒟蒻关于斜率优化DP简单的总结
  6. luoguP2711 小行星
  7. [BZOJ4195] [NOI2015] 程序自动分析 (并查集)
  8. PHP接收android传过来的图片
  9. RPC vs RESTful
  10. Linux环境下jdk1.8压缩包下载