-1
我的模板正在輸出以下內容。它沒有拉任何查詢值,但page loads fine
和doesnt fail
,但它沒有顯示任何values
。在燒瓶模板中訪問這些字典值
我在mysqlmonitor
中檢查了query
,並且它按照它應該拉的3條記錄。
<li><a href="http://blog.mysite.com/wordpress///"></a></li>
在templates/index.html
我:
{% for blogpost in blogposts %}
<li><a href="http://blog.mysite.com/wordpress/{{blogpost[2]}}/{{blogpost[3]}}/{{blogpost[1]}}">{{blogpost[0]}}</a></li>
{% else %}
<li>no blog posts right now...</li>
{% endfor %}
app.py
有這樣的:
import pymysql.cursors
app = Flask(__name__)
connection = pymysql.connect(host='localhost', user='myuser', port=3306, password='mypass', db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
@app.route('/', methods=('GET', 'POST'))
def email():
form = EmailForm()
curs = connection.cursor()
curs.execute("SELECT post_title, post_name, YEAR(post_date) as YEAR, MONTH(post_date) as MONTH FROM mydb.wp_posts WHERE post_status='publish' ORDER BY RAND() LIMIT 3")
blogposts = curs.fetchall()
if request.method == 'POST':
return render_template('index.html', form=form, blogposts=blogposts)
if __name__ == '__main__':
app.run()
UPDATE我覺得我for()
無法正常工作,因爲當我在更新template
我得到的所有數據如:
[{u'MONTH': 12, u'YEAR': 2016, u'post_name': u'data is here', u'post_title': u'data is here'},
{u'MONTH': 12, u'YEAR': 2016, u'post_name': u'data is here', u'post_title': u"data is here"}]
如何我在燒瓶template
訪問這些數據?
非常感謝!