import mysqlconnectorfrom flask import Flask render_template request redirect url_for sessionimport timeapp = Flask__name__# Set the secret key for the sessionappsecret_key = secretkey# Connect to MyS
在用户信息表中添加以下列:
systolic:收缩压(整数)diastolic:舒张压(整数)heartRate:心率(整数)sleepQuality:睡眠质量(每天睡眠时间,单位:小时)(整数)dietHabits:饮食习惯(字符串)dietHabitsProportion:饮食习惯比例(字典)
在更新用户信息的功能中,添加对以上列的处理,包括获取用户输入的值并更新到数据库中。同时,在渲染用户信息页面时,将这些列的值传递给模板,以便在页面上显示。
以下是更新用户信息功能的修改部分代码示例:
@app.route('/userinform', methods=['GET', 'POST'])
def userinform():
# Check if the user is logged in
if 'username' in session:
if request.method == 'POST':
# Get the user's updated information from the form
name = request.form['name']
age = int(request.form['age'])
height = int(request.form['height'])
weight = int(request.form['weight'])
newpassword = request.form['newpassword']
systolic = int(request.form['systolic'])
diastolic = int(request.form['diastolic'])
heartRate = int(request.form['heartRate'])
sleepQuality = int(request.form['sleepQuality'])
dietHabits = request.form.getlist('dietHabits')
dietHabitsProportion = {
'vegetables': int(request.form['vegetablesProportion']),
'fruits': int(request.form['fruitsProportion']),
'meat': int(request.form['meatProportion']),
'fish': int(request.form['fishProportion']),
'dairy': int(request.form['dairyProportion']),
'grains': int(request.form['grainsProportion']),
'nuts': int(request.form['nutsProportion']),
'sugar': int(request.form['sugarProportion']),
'friedFood': int(request.form['friedFoodProportion']),
'processedFood': int(request.form['processedFoodProportion'])
}
# Update the user's information in the database
mycursor.execute("UPDATE users SET name = %s, age = %s, height = %s, weight = %s, systolic = %s, diastolic = %s, heartRate = %s, sleepQuality = %s, dietHabits = %s, dietHabitsProportion = %s WHERE username = %s",
(name, age, height, weight, systolic, diastolic, heartRate, sleepQuality, dietHabits, dietHabitsProportion, session['username']))
# Update the user's password if a new password is provided
if newpassword:
mycursor.execute("UPDATE users SET password = %s WHERE username = %s",
(newpassword, session['username']))
mydb.commit()
# Get the updated user's information from the database
mycursor.execute("SELECT * FROM users WHERE username = %s", (session['username'],))
user = mycursor.fetchone()
# Pass the user's information to the template
return render_template('userinform.html', user=user)
else:
# Get the user's information from the database
mycursor.execute("SELECT * FROM users WHERE username = %s", (session['username'],))
user = mycursor.fetchone()
# Pass the user's information to the template
return render_template('userinform.html', user=user)
else:
return redirect(url_for('home'))
在用户信息页面的模板中,添加相应的表单字段用于接收用户输入,并在原有的表单中添加对应的列的值。同时,在保存按钮的点击事件中,将新添加的表单字段的值一同提交到服务器。
<form action="/userinform" method="POST">
<!-- 用户信息表单字段 -->
<label for="bloodPressure">血压(mmHg):</label>
收缩压:
<input type="number" id="systolic" name="systolic" value="{{ user[8] }}">
舒张压:
<input type="number" id="diastolic" name="diastolic" value="{{ user[9] }}">
<br>
<label for="heartRate">心率(BPM):</label>
<input type="number" id="heartRate" name="heartRate" value="{{ user[10] }}">
<br>
<label for="sleepQuality">睡眠质量(每天睡眠时间,单位:小时):</label>
<input type="number" id="sleepQuality" name="sleepQuality" value="{{ user[11] }}">
<br>
<label for="dietHabits">饮食习惯:</label><br>
<input type="checkbox" id="vegetables" name="dietHabits" value="vegetables" {{ 'checked' if 'vegetables' in user[12] }}>蔬菜:
<input type="number" id="vegetablesProportion" name="dietHabitsProportion" value="{{ user[13]['vegetables'] }}">
%<br>
<!-- 其他饮食习惯字段 -->
...
<br>
<input type="submit" value="保存">
<a href="/" class="button">返回</a>
</form>
注意:以上代码示例仅供参考,具体实现可能需要根据实际需求进行调整
原文地址: http://www.cveoy.top/t/topic/hYrV 著作权归作者所有。请勿转载和采集!