Flask App不会加载app.py(提供的文件/路径(app)似乎不存在) [英] Flask App will not load app.py (The file/path provided (app) does not appear to exist)
问题描述
我的烧瓶应用程序正在为()块的输出'no content',我不知道为什么。
My flask app is outputting 'no content' for the for()
block and i dont know why.
我在 app.py
中测试了我的查询,这里是 app.py
:
I tested my query in app.py
, here is 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
的样子。
以下是 templates /
中的 index.html
:
<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>
我检查了这个查询,并返回所需的输出:
I checked the query, and it returns desired output like:
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
如果我尝试通过运行<$ c $重启dev服务器c> export FLASK APP = app.py',然后是 flask run`,我得到一个错误:
If i try to restart the dev server by running export FLASK APP=app.py', then
flask run`, i get an error of:
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
- 这也给出同样的错误。
I've also tried running via export FLASK_APP=app.py
then python -m flask run
- this also gives the same error.
想法如何解决?
推荐答案
您的模板中没有任何内容称为 blogposts
。您需要使用关键字参数传递数据:
You haven't got anything in your template called blogposts
. You need to use keyword arguments to pass the data:
return render_template('index.html', blogposts=blogposts)
另请注意,您应该在函数内部真正执行该查询,否则只会执行进程启动,总是有同样的三个职位。
Also note you should really do that query inside the function, otherwise it will only ever execute on process start and you'll always have the same three posts.
这篇关于Flask App不会加载app.py(提供的文件/路径(app)似乎不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!