【发布时间】:2016-12-30 22:27:50
【问题描述】:
我的烧瓶应用程序正在为 for() 块输出“无内容”,我不知道为什么。
我在app.py 中测试了我的查询,这里是app.py:
# mysql config
app.config['MYSQL_DATABASE_USER'] = 'user'
app.config['MYSQL_DATABASE_PASSWORD'] = 'mypass'
app.config['MYSQL_DATABASE_DB'] = 'mydbname'
app.config['MYSQL_DATABASE_HOST'] = 'xxx.xxx.xxx.xxx'
mysql = MySQL()
mysql.init_app(app)
c = mysql.connect().cursor()
blogposts = c.execute("SELECT post_title, post_name FROM mydbname.wp_posts WHERE post_status='publish' ORDER BY RAND() LIMIT 3")
@app.route('/', methods=('GET', 'POST'))
def email():
form = EmailForm()
if request.method == 'POST':
if form.validate() == False:
return 'Please fill in all fields <p><a href="/">Try Again</a></p>'
else:
msg = Message("Message from your visitor",
sender='contact@site.com',
recipients=['contact@site.com'])
msg.body = """
From: %s
""" % (form.email.data)
mail.send(msg)
#return "Successfully sent message!"
return render_template('email_submit_thankyou.html')
elif request.method == 'GET':
return render_template('index.html', blogposts)
这就是我的app.py 的样子。
下面是我的index.htmltemplates/:
<ul>
{% for blogpost in blogposts %}
<li><a href="{{blogpost[1]}}">{{blogpost[0]}}</a></li>
{% else %}
<li>no content...</li>
{% endfor %}
<div class="clearL"> </div>
</ul>
我检查了查询,它返回所需的输出,例如:
ID post_title post_name
1 a title here a-title-here
2 a title here2 a-title-here2
3 a title here3 a-title-here3
如果我尝试通过运行 export FLASK APP=app.py', thenflask run` 来重新启动开发服务器,我会收到以下错误:
Error: The file/path provided (app) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py
我也尝试通过export FLASK_APP=app.py 然后python -m flask run 运行 - 这也给出了同样的错误。
思考如何解决?
【问题讨论】: