帮我实现一个美观的对称的有登陆失败提示的中文html登陆页面代码,支持session验证,后端是tornado
时间: 2023-05-11 07:06:48 浏览: 91
好的,我可以为您提供一个美观的对称的有登陆失败提示的中文html登陆页面代码,支持session验证,后端是tornado。以下是代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
<style>
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}
.container {
margin: 0 auto;
width: 400px;
padding: 20px;
background-color: #fff;
border: 1px solid #ccc;
box-shadow: 0 0 10px #ccc;
}
h1 {
text-align: center;
font-size: 24px;
margin-bottom: 20px;
}
form {
display: flex;
flex-direction: column;
align-items: center;
}
label {
display: block;
margin-bottom: 10px;
font-size: 16px;
font-weight: bold;
}
input[type="text"], input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
input[type="submit"] {
background-color: #4CAF50;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 5px;
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
import tornado.web
import tornado.ioloop
import tornado.escape
class LoginHandler(tornado.web.RequestHandler):
def get(self):
self.render("login.html", error=None)
def post(self):
username = self.get_argument("username")
password = self.get_argument("password")
if username == "admin" and password == "123456":
self.set_secure_cookie("user", tornado.escape.json_encode({"username": username}))
self.redirect("/")
else:
self.render("login.html", error="用户名或密码错误")
class MainHandler(tornado.web.RequestHandler):
def get_current_user(self):
user_json = self.get_secure_cookie("user")
if not user_json:
return None
return tornado.escape.json_decode(user_json)
@tornado.web.authenticated
def get(self):
self.write("欢迎," + self.current_user["username"])
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/login", LoginHandler),
]
settings = {
"cookie_secret": "your_secret_key_here",
"login_url": "/login",
"template_path": "templates",
"static_path": "static",
"xsrf_cookies": True,
}
super(Application, self).__init__(handlers, **settings)
if __name__ == "__main__":
app = Application()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
```
请注意,您需要在 `settings` 中设置 `cookie_secret`,这是用于加密 cookie 的密钥。您还需要在 `LoginHandler` 中设置正确的用户名和密码,以便进行验证。
阅读全文