首页 > 解决方案 > 将多个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 %}

如果有人可以帮助我解决这个大问题,那将是很棒的!

标签: pythonmysqlsqlflask

解决方案


你想要类似的东西:

{% for score in scores %}
     <tr> {{ score }} </tr>
{% endfor %}

?


推荐阅读