python - 将多个sql行放入一个html文件
问题描述
所以我想要做的是我试图获得一行并将其作为一个列表放在 html 上,如下所示
{{ row[1] }}<br>
{{ row[2] }}<br>
{{ row[3] }}<br>
但我不想遍历每一行,因为会有 100 行,所以我想知道是否有更简单的解决方案来解决这个问题
代码:
@app.route('/u/<int:userid>&m=<int:mode>&rx=<int:relax>')
def profile(userid, mode, relax):
# Check if user is loggedin
# We need all the account info for the user so we can display it on the profile page
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM users WHERE id = %s', (userid,))
userinfo = cursor.fetchone()
username = userinfo['username']
if relax == 0:
cursor.execute('SELECT * FROM users_stats WHERE username = %s', (username,))
userinfoscore = cursor.fetchone()
cursor.execute('SELECT * FROM scores WHERE userid = %s AND completed = 3 AND play_mode = 0 or userid = %s AND completed = 2 AND play_mode = 0 ORDER BY pp DESC', (userid, userid))
scores = cursor.fetchall()
elif relax == 1:
cursor.execute('SELECT * FROM rx_stats WHERE username = %s', (username,))
userinfoscore = cursor.fetchone()
results = cursor.execute('SELECT * FROM scores_relax WHERE userid = %s AND completed = 3 AND play_mode = 0 or userid = %s AND completed = 2 AND play_mode = 0 ORDER BY pp DESC', (userid, userid))
scores = cursor.fetchall()
print(results)
if 'loggedin' in session:
return render_template('profile.html', userinfo=userinfo, userid=userid, stats=userinfoscore, username=session['username'], yourid=session['id'], round=round, scores=scores)
else:
return render_template('profile.html', userinfo=userinfo, userid=userid, stats=userinfoscore, username='Guest', yourid=999, round=round, scores=scores)
.html 文件:
{% extends 'layout.html' %}
{% block title %}Profile{% endblock %}
{% block content %}
<h2>Profiles!</h2>
<div>
<table>
{% if session['username'] == userinfo['username'] %}
<tr>
<td>
<a href="https://new.skysu.ga/settings"><button>Edit Profile!</button></a>
</td>
</tr>
{% else %}
{% endif %}
<tr>
<td>
<img src="https://a.skysu.ga/{{ userid }}.png">
</td>
</tr>
<tr>
<td>{{ userinfo['username'] }} is a Chiyo player from the {{ stats['country'] }}</td>
</tr>
<tr>
<td>
<a href="https://new.skysu.ga/u/{{ userid }}&m=0&rx=1"><button>Relax</button></a> <a href="https://new.skysu.ga/u/{{ userid }}&m=0&rx=0"><button>Regular </button></a>
</td>
</tr>
<tr>
<td>
PP: {{ stats['pp_std'] }}<br>
Playcount: {{ stats['playcount_std'] }}<br>
Ranked Score: {{ stats['ranked_score_std'] }}<br>
Total Score: {{ stats['total_score_std'] }}<br>
Replays Watched: {{ stats['replays_watched_std'] }}<br>
Total Hits: {{ stats['total_hits_std'] }}<br>
Level: {{ stats['level_std'] }}<br>
Accuracy: {{ round(stats['avg_accuracy_std'], 2) }}%<br>
</td>
</tr
<tr>
<td>{{ scores }}</td>
</tr>
</table>
</div>
{% endblock %}
如果有人可以帮助我解决这个大问题,那将是很棒的!
解决方案
你想要类似的东西:
{% for score in scores %}
<tr> {{ score }} </tr>
{% endfor %}
?
推荐阅读
- r - For循环:如何打印剩余的序列?
- c++ - 一个数的最大除数和质因数的关系
- amazon-web-services - 从 Pyspark 调用 AWS S3 存储桶时出错。AWS 错误代码:空,AWS 错误消息:错误请求
- reactjs - WebpackError:TypeError:无法解构属性
- asp.net-core - 只为自定义日志写入日志
- static-analysis - 使用 clang-tidy 时忽略某些文件
- android - android Retrofit中的动态JSON解析?
- linux - 我有多个不同版本的文件名。如何从每个文件名中选择最高编号的版本
- reactjs - goBack() 按钮不起作用。HashRouter 反应
- plot - 单个图上的多个 Y 轴