在我的网络应用程序中,一旦管理员登录,它就会显示所有用户测验的所有结果。每列中都会显示每个测验的姓名、电子邮件和分数。但是当我尝试运行时,它只显示: 结果
名称 电子邮件 网络基础知识 网络工具和测试设备 网络电缆
并且没有数据显示
这是 models.py 中的表格:
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(150), unique=True)
password = db.Column(db.String(150))
first_name = db.Column(db.String(150))
class Course(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
class Fundamentals_of_Networking_score(db.Model):
id = db.Column(db.Integer, primary_key=True)
scores = db.Column(db.Integer)
class Network_Tools_and_Testing_Devices_score(db.Model):
id = db.Column(db.Integer, primary_key=True)
scores = db.Column(db.Integer)
class Network_Cables_score(db.Model):
id = db.Column(db.Integer, primary_key=True)
scores = db.Column(db.Integer)
这是 view.py 的片段:
@views.route("/showall", methods=["GET"])
def show_all():
# Query all users along with their scores
users_with_scores = db.session.query(User.first_name, User.email,
Fundamentals_of_Networking_score.scores,
Network_Tools_and_Testing_Devices_score.scores,
Network_Cables_score.scores) \
.join(Fundamentals_of_Networking_score, User.id == Fundamentals_of_Networking_score.id) \
.join(Network_Tools_and_Testing_Devices_score, User.id == Network_Tools_and_Testing_Devices_score.id) \
.join(Network_Cables_score, User.id == Network_Cables_score.id) \
.all()
# Convert the query result into a list of dictionaries
scores_list = []
for user_data in users_with_scores:
scores_list.append({
'first_name': user_data[0],
'email': user_data[1],
'Fundamentals_of_Networking_score': user_data[2],
'Network_Tools_and_Testing_Devices_score': user_data[3],
'Network_Cables_score': user_data[4]
})
print(scores_list) # Add this line to check the content of scores_list
return render_template("showall.html", scores_list=scores_list, user=None)
here the snippet of showall.html code
<div class="card-header">
<label style=" font-size: 1.5rem;">Results</label>
</div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Fundamentals of Networking</th>
<th>Network Tools and Testing Devices</th>
<th>Network Cables</th>
</tr>
</thead>
<tbody>
{% for user_scores in scores_list %}
<tr>
<td>{{ user_scores.first_name }}</td>
<td>{{ user_scores.email }}</td>
<td>{{ user_scores.Fundamentals_of_Networking_score }}</td>
<td>{{ user_scores.Network_Tools_and_Testing_Devices_score }}</td>
<td>{{ user_scores.Network_Cables_score }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
使用以下 html 更新您的 showall.html
<div class="card-header">
<label style=" font-size: 1.5rem;">Results</label>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Fundamentals of Networking</th>
<th>Network Tools and Testing Devices</th>
<th>Network Cables</th>
</tr>
</thead>
<tbody>
{% for user_scores in scores_list %}
<tr>
<td>{{ user_scores.first_name }}</td>
<td>{{ user_scores.email }}</td>
<td>{{ user_scores.Fundamentals_of_Networking_score }}</td>
<td>{{ user_scores.Network_Tools_and_Testing_Devices_score }}</td>
<td>{{ user_scores.Network_Cables_score }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>