【发布时间】: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