目錄
1.基礎知識?編輯
2.腳手架(模版)
3.登錄流程圖(processon)
4.登錄表單
4.1后(返回值)任何值:username/password
(4.1.1)app.py
(4.1.2)表單.html
(4.3)login.py(沒啥用,充當第二頁的跳轉頁面)
4.1.3返回 用戶名+密碼 結果
4.2寫死 admin/123 ##,返回index首頁
(4.2.1app.py)用戶名/密碼正確,返回index首頁,否則,返回error404_n頁面
(4.2.2index.html)首頁
(4.2.3error.html)404_n
5.注冊表單(后臺)?
(5.1 app.py的注冊部分)
(5.2 register.html 注冊表)
?報錯:
**404:沒有寫路由
**500:目錄結構 / 內容錯誤
1.基礎知識
Linux寫法
2.腳手架(模版)
(1)打印hello world
#!/usr/bin/env python
#-*- coding:utf-8 -*-from tornado import web, httpserver, ioloop
#1.邏輯處理模塊
class LoginHandler(web.RequestHandler):def get(self,*args,**kwargs):self.write("hello world")#self.render("login.html")#2.路由
application=web.Application([(r"/login",LoginHandler),])#3.socket服務端
if __name__=='__main__':http_server=httpserver.HTTPServer(application)print("http://127.0.0.1:8080/login")http_server.listen(8080)ioloop.IOLoop.current().start()
(2)網頁login
1).py
self.render("login.html") #渲染
2)? ?.html?
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1 style="color:pink;">hi</h1>
</body>
</html>
**報錯500,目錄結構
3.登錄流程圖(processon)
4.登錄表單
4.1后(返回值)任何值:username/password
**基于這個前端頁面,用tornado框架,寫后端api接口代碼,調用get、post請求。
(4.1.1)app.py
#!/usr/bin/env python
#-*- coding:utf-8 -*-from tornado import web, httpserver, ioloop
#1.邏輯處理模塊
class LoginHandler(web.RequestHandler):def get(self,*args,**kwargs):#self.write("hello world")#返回登錄頁面#self.render("login.html") #渲染self.render("表單.html") #渲染def post(self,*args,**kwargs):#驗證用戶密碼(獲取)username=self.get_argument("username")passward=self.get_argument("password")print(username,passward)#設置
setting={'template_path':'template'
}#2.路由
application=web.Application([(r"/login",LoginHandler), #這個對應著/login],**setting)#3.socket服務端
if __name__=='__main__':http_server=httpserver.HTTPServer(application)print("http://127.0.0.1:8080/login")http_server.listen(8080)ioloop.IOLoop.current().start()
(4.1.2)表單.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>表單</title>
</head>
<body><h2>表單</h2><form action="/login" method="post"> <!--這里的、login對應著app.py里面的路由-->賬號:<input type="text" name="username"/><br/>密碼:<input type="password" name="password"/><br/><input type="submit" value="登錄"/></form></body>
</html>
(4.3)login.py(沒啥用,充當第二頁的跳轉頁面)
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1 style="color:pink;">hi</h1>
</body>
</html>
4.1.3返回 用戶名+密碼 結果
4.2寫死 admin/123 ##,返回index首頁
(4.2.1app.py)用戶名/密碼正確,返回index首頁,否則,返回error404_n頁面
#!/usr/bin/env python
#-*- coding:utf-8 -*-from tornado import web, httpserver, ioloop
#1.邏輯處理模塊
#登錄
class LoginHandler(web.RequestHandler):def get(self,*args,**kwargs):#self.write("hello world")#返回登錄頁面#self.render("login.html") #渲染self.render("表單.html") #渲染def post(self,*args,**kwargs):#驗證用戶密碼(獲取)username=self.get_argument("username")passward=self.get_argument("password")#print(username,passward)#寫死,if正確進入if username=='admin' and passward=='123':self.redirect('/index')##跳轉else:self.render('error.html')##跳轉首頁面模塊
class IndexHandler(web.RequestHandler):def get(self,*args,**kwargs):self.render("index.html")#設置
setting={'template_path':'template'
}#2.路由
application=web.Application([(r"/login",LoginHandler), #這個對應著/login(r"/index",IndexHandler),##首頁面路由],**setting)#3.socket服務端
if __name__=='__main__':http_server=httpserver.HTTPServer(application)print("http://127.0.0.1:8080/login")http_server.listen(8080)ioloop.IOLoop.current().start()
(4.2.2index.html)首頁
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>login2</title>
</head>
<body><h2 style="color:pink">I am index</h2></body>
</html>
(4.2.3error.html)404_n
<h2 style="color:purple">輸入錯誤404_n</h2>
5.注冊表單(后臺)?
(5.1 app.py的注冊部分)
###全局變量
user_info={}
###注冊 post獲取
class RegisterHandler(web.RequestHandler):def post(self,*args,**kwargs):email = self.get_argument("emial")username = self.get_argument("username")password = self.get_argument("password")password1 = self.get_argument("password1")access = self.get_argument("access", default=None)if access: #用email當keyif password==password1:UNER_INFO[email]={"username":username,"password":password,}self.render("success.html",info={'stauts':True,'info':'注冊成功','second':3})else:self.render('error.html', info={'status': False,'info': '密碼不一致','second': 3, # 倒計時3秒'url': '/register'}) # 跳轉else:self.render('error.html', info={'status': False,'info': '請同意協議','second': 3, # 倒計時3秒'url':'/register'}) # 跳轉def get(self,*args,**kwargs):self.render("表單.html",type='register')###收到數據后要存儲起來
###用數據庫/全局變量(√)上面
(5.2 register.html 注冊表)
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>用戶注冊</title><style>body {font-family: Arial, sans-serif;background-color: #f4f4f4;display: flex;justify-content: center;align-items: center;height: 100vh;margin: 0;}.container {background-color: white;padding: 20px;border-radius: 5px;box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);width: 300px;}.container h2 {text-align: center;margin-bottom: 20px;}.form-group {margin-bottom: 15px;}.form-group label {display: block;margin-bottom: 5px;}.form-group input[type="email"],.form-group input[type="text"],.form-group input[type="password"] {width: 100%;padding: 8px;box-sizing: border-box;border: 1px solid #ccc;border-radius: 4px;}.form-group input[type="checkbox"] {margin-right: 5px;}.form-group button {width: 100%;padding: 10px;background-color: #4CAF50;color: white;border: none;border-radius: 4px;cursor: pointer;}.form-group button:hover {background-color: #45a049;}.form-group .reset {background-color: #ccc;}.form-group .reset:hover {background-color: #bbb;}</style>
</head>
<body><div class="container"><h2>用戶注冊</h2><form><div class="form-group"><label for="email">郵箱</label><input type="email" id="email" name="email" required></div><div class="form-group"><label for="username">用戶名</label><input type="text" id="username" name="username" value="admin" required></div><div class="form-group"><label for="password">密碼</label><input type="password" id="password" name="password" required></div><div class="form-group"><label for="confirm-password">確認密碼</label><input type="password" id="confirm-password" name="confirm-password" required></div><div class="form-group"><input type="checkbox" id="agreement" name="agreement" required><label for="agreement">接受 用戶協議</label></div><div class="form-group"><button type="reset" class="reset">重置</button><button type="submit">注冊</button></div></form></div>
</body>
</html>