所以,这是我的代码,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迫使我输入更多的非代码来提交这个问题,所以......
AND
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>
答案 0 :(得分:0)
您似乎正在使用关键字参数Model
将baseball
列表传递到模板中。但是,在您的模板中,您尝试使用名称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
标记内直接按名称引用属性。