Tornado 网站demo 三
2021-07-13 05:04
标签:methods use bsp 信息 ado line 跳转 users script 修改index.py readdb.py 添加select_columns 方法 修改index.html文件 要求用户正确登录之后,跳转到另外一个页面,并且在那个页面中显示出用户的完整信息。 先修改 url.py 文件,在其中增加一些内容 然后就建立 handlers/user.py 文件 Tornado 网站demo 三 标签:methods use bsp 信息 ado line 跳转 users script 原文地址:http://www.cnblogs.com/Erick-L/p/7079870.html模板
#!/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.")
def select_columns(table, column ):
sql = "select " + column + " from " + table
cur.execute(sql)
lines = cur.fetchall()
return lines
DOCTYPE html>
head>
meta charset="UTF-8">
meta name="viewport" content="width=device-width, initial-scale=1" />
title>Learning Pythontitle>
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>
#!/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),
]
#!/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)