python网页系统登陆界面
时间: 2023-11-29 20:04:13 浏览: 51
你可以使用 Python 的 Flask 框架来创建一个简单的网页系统登陆界面。以下是一个简单的示例代码:
```python
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if username == 'admin' and password == 'password':
return redirect(url_for('dashboard'))
else:
error = 'Invalid Credentials. Please try again.'
return render_template('login.html', error=error)
return render_template('login.html')
@app.route('/dashboard')
def dashboard():
return 'Welcome to the Dashboard!'
if __name__ == '__main__':
app.run(debug=True)
```
在上面的代码中,我们创建了一个 Flask 应用程序,并定义了两个路由。第一个路由是用于登陆的页面,它支持 GET 和 POST 请求。当用户提交表单时,我们从表单中获取用户名和密码,并检查它们是否正确。如果验证通过,我们将用户重定向到仪表板页面。否则,我们将显示一个错误消息。
第二个路由是仪表板页面,它只是简单地显示一个欢迎消息。在本例中,我们只是在控制台中输出了消息,但你可以轻松地将其转换为一个 HTML 模板。
最后,我们可以使用 `app.run()` 方法来启动服务器。在本例中,我们设置了 `debug=True`,以便在开发过程中获取更多有用的调试信息。
你还需要创建一个 HTML 模板来显示登陆表单。以下是一个简单的示例代码:
```html
<!doctype html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Login to Your Account</h1>
{% if error %}
<p>{{ error }}</p>
{% endif %}
<form method="post">
<label>Username:</label>
<input type="text" name="username"><br>
<label>Password:</label>
<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
在上面的代码中,我们创建了一个简单的表单,其中包含两个输入字段(用户名和密码)和一个提交按钮。当表单被提交时,它将发送一个 POST 请求,我们将在 Python 代码中处理它。
你可以根据自己的需要对此 HTML 模板进行修改和自定义。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)