更新体重不显示新体重
时间: 2024-11-25 09:17:40 浏览: 20
根据您提供的文档内容,如果更新体重后页面没有显示最新的体重数据,可能有以下几个原因:
1. **缓存问题**:
- 浏览器可能会缓存旧的数据。尝试清除浏览器缓存或强制刷新页面(通常是按 `Ctrl + F5`)。
2. **数据库提交问题**:
- 确保在更新体重时,数据库事务已经正确提交。检查 `update_pet_weight` 路由中的 `conn.commit()` 是否被正确调用。
3. **查询问题**:
- 在 `view_pet` 路由中,确保从数据库中查询到的是最新的体重数据。可以检查 SQL 查询语句是否正确。
4. **模板渲染问题**:
- 确保在 `view_pet.html` 中正确渲染了体重数据。检查模板文件中的循环和变量引用是否正确。
### 具体检查步骤
1. **检查数据库提交**:
```python
@app.route('/update_pet_weight/<int:pet_id>', methods=['GET', ' POST'])
def update_pet_weight(pet_id):
if 'user_id' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
weight = float(request.form['weight'])
conn = connect_db()
cursor = conn.cursor()
cursor.execute('INSERT INTO pet_weights (pet_id, weight) VALUES (?, ?)', (pet_id, weight))
conn.commit() # 确保这里正确提交事务
flash("宠物体重更新成功!", 'success')
logging.info("宠物体重更新成功!")
conn.close()
return redirect(url_for('view_pet', pet_id=pet_id))
return render_template('update_pet_weight.html', pet_id=pet_id)
```
2. **检查查询语句**:
```python
@app.route('/view_pet/<int:pet_id>')
def view_pet(pet_id):
if 'user_id' not in session:
return redirect(url_for('login'))
conn = connect_db()
cursor = conn.cursor()
cursor.execute('SELECT pet_name FROM pets WHERE id = ?', (pet_id,))
pet = cursor.fetchone()
if not pet:
flash("宠物不存在!", 'warning')
logging.warning("宠物不存在!")
return redirect(url_for('dashboard'))
cursor.execute('SELECT weight, recorded_at FROM pet_weights WHERE pet_id = ? ORDER BY recorded_at', (pet_id,))
weights = cursor.fetchall()
conn.close()
return render_template('view_pet.html', pet=pet, weights=weights)
```
3. **检查模板文件**:
```html
<!-- view_pet.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ pet[0] }} 的体重记录</title>
</head>
<body>
<h1>{{ pet[0] }} 的体重记录</h1>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<ul>
{% for category, message in messages %}
<li class="{{ category }}">{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<a href="{{ url_for('update_pet_weight', pet_id=pet[0][0]) }}">更新体重</a>
<table>
<tr>
<th>体重 (kg)</th>
<th>记录时间</th>
</tr>
{% for weight in weights %}
<tr>
<td>{{ weight[0] }}</td>
<td>{{ weight[1] }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
```
通过以上步骤,您可以逐步排查并解决更新体重后页面未显示最新体重的问题。希望这些信息对您有所帮助!
阅读全文
相关推荐


















