帮我实现一个美观的中文html登陆页面,支持session验证,后端是tornado,给出代码示例
时间: 2023-05-13 07:05:23 浏览: 123
好的,以下是一个简单的示例代码,实现了一个美观的中文html登陆页面,支持session验证,后端使用tornado框架:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登陆页面</title>
<style>
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}
.container {
margin: 50px auto;
width: 400px;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
h1 {
text-align: center;
font-size: 24px;
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 10px;
font-size: 16px;
}
input[type="text"], input[type="password"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 3px;
font-size: 16px;
margin-bottom: 20px;
}
input[type="submit"] {
background-color: #4CAF50;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 3px;
font-size: 16px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #3e8e41;
}
.error {
color: red;
font-size: 14px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>登陆</h1>
<form action="/login" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="登陆">
<div class="error">{{ error }}</div>
</form>
</div>
</body>
</html>
```
Python代码:
```python
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("login.html", error="")
def post(self):
username = self.get_argument("username")
password = self.get_argument("password")
if username == "admin" and password == "123456":
self.set_secure_cookie("user", username)
self.redirect("/welcome")
else:
self.render("login.html", error="用户名或密码错误")
class WelcomeHandler(tornado.web.RequestHandler):
def get(self):
user = self.get_secure_cookie("user")
if user:
self.write("欢迎," + user.decode())
else:
self.redirect("/")
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/login", MainHandler),
(r"/welcome", WelcomeHandler),
]
settings = {
"cookie_secret": "my_secret_key",
"template_path": "templates",
"static_path": "static",
"xsrf_cookies": True,
"debug": True,
}
super().__init__(handlers, **settings)
if __name__ == "__main__":
app = Application()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
```
这个示例代码实现了一个简单的登陆页面,用户输入用户名和密码后,如果正确就会设置一个session cookie,并跳转到欢迎页面,否则会在登陆页面显示错误信息。后端使用tornado框架,模板引擎使用了jinja2,可以在templates目录下创建login.html模板文件。
阅读全文