【问题标题】:Data not passing into flask/jinja template from sql database数据未从 sql 数据库传递到 flask/jinja 模板
【发布时间】:2017-08-01 12:44:06
【问题描述】:

所以,这是我的代码,html 页面呈现,但没有数据库,它只是链接和图像......有点卡住了。数据库已经存在,我只想显示它。我尝试遍历模板中的数据库,但没有任何显示。

main.py:

from flask import Flask, request, redirect, render_template, flash
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://baseball:baseball@localhost:8889/baseball'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)


class starting_pitchers(db.Model):

    Name = db.Column(db.String(80), unique=True)
    ID = db.Column(db.Integer, primary_key=True)
    CFIP = db.Column(db.Integer)
    xFIP = db.Column(db.Float)
    FIP = db.Column(db.Float)
    KperBB = db.Column(db.Float)
    Total_Ks = db.Column(db.Integer)
    WHIP = db.Column(db.Float)
    ERA = db.Column(db.Float)
    Innings_Pitched = db.Column(db.Float)
    Wins = db.Column(db.Integer)
    Quality_Start_Rate = db.Column(db.Integer)
    '''Swg_Strike_Rate = db.Column(db.Float)'''
    Ground_Ball_Rate = db.Column(db.Float)
    Soft_Contact_Rate = db.Column(db.Float)
    FP_Rank = db.Column(db.Integer, unique=True)
    SW_Rank = db.Column(db.Integer)
    CT_Rank = db.Column(db.Integer)
    HC_Rank = db.Column(db.Integer)

    def __init__(self, ID, Name):
        self.Name = Name
        self.ID = ID


@app.route("/", methods=['GET', 'POST'])
def index():

    baseball = starting_pitchers.query.all()

    return render_template('index.html', title="Baseball", baseball=baseball)

if __name__ == '__main__':
    app.run()

所以,这是主要的 python 文件...我意识到这部分是不必要的,但是 stackoverflow 迫使我输入更多的非代码来提交这个问题,所以...

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Baseball01</title>
        <link rel="stylesheet" href="/css/normalize.css">
        <link rel="stylesheet" href="/css/styles.css">
    </head>

<body>
<h1>Fantasy Pitcher Quick Info</h1>

<img src="C:\Users\Kristen%20Tuomey\Pictures\Saved%20Pictures\kershaw.jpg" width="400" height="300">

<p><h2> 2017</h2></p>

<h4><a href=http://www.brooksbaseball.net/pfxVB/pfx.php>PITCHf/x-Brooks Baseball</a></h4>

<p>  Used to view a particular pitcher's performance in a particular game.  Select the date, then the game, then the pitcher.  Very useful for judging recent results against prior results and gauging the reason for any changes.  For example, has this pitcher's velocity gone up or down?  Which pitch is most effective, and how often is he throwing it?</p>

<h4><a href=http://www.baseballprospectus.com/sortable/index.php?cid=2022356>CFIP-Baseball Prospectus</a></h4>

<p>  Predictive, rather than descriptive, global pitching statistic</p>

<h4>  <a href=http://www.fangraphs.com/leaders.aspx?pos=all&stats=pit&lg=all&qual=0&type=1&season=2017&month=0&season1=2017&ind=0&team=0&rost=0&age=0&filter=&players=p2017-05-08&sort=19,d>ERA minus FIP</a></h4>

<p>  ERA minus FIP is the expected regression a pitcher is likely to undergo.  A larger number generally forecasts greater positive regression, and a negative number indicates negative expected regression.</p>

<table border="1" cellpadding="5" cellspacing="5">
    {% for row in starting_pitchers %}
        <tr>
        {% for d in row %}
            <td>{{ d }}</td>
        {% endfor %}
        </tr>
    {% endfor %}
</table>

</body>

</html>

【问题讨论】:

    标签: python html mysql templates flask


    【解决方案1】:

    看起来您正在使用关键字参数baseballModels 的列表传递给您的模板。但是,在您的模板中,您尝试通过名称 starting_pitchers 来引用它。

    我认为打算做的更像是以下内容:

    {% for row in baseball %}
        <tr>
            <td>{{ row.Name }}</td>
            <td>{{ row.ID }}</td>
            <td>{{ row.CFIP }}</td>
            ...
        </tr>
    {% endfor %}
    

    由于starting_pitchers 类型不可迭代,我们移除了内部for 循环并在td 标记内直接按名称引用属性。

    【讨论】:

    • 文件“C:\Users\Kristen Tuomey\lc101\baseball-project\templates\index.html”,第 32 行,顶级模板代码 {% for d in row %} 类型错误: “starting_pitchers”对象不可迭代
    • 非常感谢任何帮助
    • 谢谢...你是圣人
    • 知道如何将列标题传递到模板中并使其可排序吗?该文档不存在,关于此特定问题的内容不多
    猜你喜欢
    • 2021-03-29
    • 2014-12-17
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多