Python使用第三方库实现网站的制作要求能接收用户姓名年龄性别信息并在页面上显示
时间: 2024-06-09 20:08:18 浏览: 4
要实现这个功能,你需要先选择一个 Python Web 框架,例如 Flask 或 Django,并安装其中一个框架的相应库。然后,你需要使用 HTML、CSS 和 JavaScript 来创建前端页面,并将其与后端代码连接起来。
以下是一个简单的示例:
1. 安装 Flask 库
```
pip install Flask
```
2. 创建 Flask 应用程序
```
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
name = request.form['name']
age = request.form['age']
gender = request.form['gender']
return render_template('result.html', name=name, age=age, gender=gender)
return render_template('index.html')
if __name__ == '__main__':
app.run()
```
3. 创建前端页面
在项目文件夹中创建 templates 文件夹,并在其中创建两个 HTML 文件:index.html 和 result.html。
index.html:
```
<!DOCTYPE html>
<html>
<head>
<title>Personal Information</title>
</head>
<body>
<form method="POST" action="/">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br>
<label for="age">Age:</label>
<input type="text" id="age" name="age"><br>
<label for="gender">Gender:</label>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
```
result.html:
```
<!DOCTYPE html>
<html>
<head>
<title>Personal Information</title>
</head>
<body>
<h1>Personal Information</h1>
<p>Name: {{ name }}</p>
<p>Age: {{ age }}</p>
<p>Gender: {{ gender }}</p>
</body>
</html>
```
4. 运行应用程序
在命令行中运行以下命令:
```
python app.py
```
然后在浏览器中打开 http://localhost:5000/,输入姓名、年龄和性别,点击提交按钮,即可在结果页面上看到用户的信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)