【问题标题】:put multiple sql rows to an html file将多个sql行放入一个html文件
【发布时间】:2020-09-09 05:52:01
【问题描述】:

所以我想要做的是我试图获取一行并将其作为列表放在 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 %}

如果有人能帮我解决这个大问题,那我就太好了!

【问题讨论】:

  • 什么禁止您以编程方式生成 html?
  • @Drako 它并没有阻止我生成 html 文件,它只是我想在每一行之后放置间距,这样它看起来就不会一团糟gyazo.com/…(请记住有像 100 或可以使用的行,所以我不能做 {{ score[1] }}
    etc
  • 只需以编程方式添加间距 - 在一些函数变量中填充您需要的任何内容,然后将其添加到 html 中,为每一行添加所有额外的空格以及您需要的任何内容
  • @Drako 我已经考虑了一段时间,只是我真的不知道该怎么做或制作一个可以为我添加间距的函数。
  • html 是字符串 - 间距可以是字符串 - 如果您不知道如何创建字符串,您应该了解一些编程基础知识 - 遵循一些在线教程或其他内容。SO 不是免费的编码服务- 我们随时为您提供帮助。

标签: python mysql sql flask


【解决方案1】:

你想要类似的东西:

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

?

【讨论】:

  • 是的,但我希望它像在一行完成后一样,我希望它在末尾放一个
    以便下一行可以放在它下面。
  • 也许是 ' {{ score }} ' 或 ' {{ score }}
    ' ?
  • 非常感谢!! &lt;tr&gt; &lt;td&gt; {{ score }} &lt;/td&gt;&lt;/tr&gt; 成功了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 2012-10-08
  • 1970-01-01
  • 2022-11-21
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
相关资源
最近更新 更多