2016-12-30 66 views
-2

我烧瓶应用正在输出“没有内容”为for()块(所提供的文件/路径(应用程序)似乎并不存在),我不知道为什么。瓶应用程序将不会加载app.py

我测试我的查询中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='[email protected]', 
          recipients=['[email protected]']) 
      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', then烧瓶运行重新启动开发服务器`,我得到一个错误:

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运行 - 这也给出了相同的错误。

思考如何解决?

回答

1

你没有得到你的模板什么叫blogposts。您需要使用关键字参数来传递数据:

return render_template('index.html', blogposts=blogposts) 

另外请注意,你应该真正做到里面的功能是查询,否则将永远只能在执行过程开始,你将永远有相同的三个职位。

+0

大后,我得到一个错误,当我运行'出口FLASK_APP = app.py',然后'烧瓶运行'它给了我一个错误'错误:提供的文件/路径(app)似乎不存在'的想法? – Jshee

+0

对不起,我不明白这个问题。你显然已经知道它运行之前,有什么改变? –

+0

我知道,但是在我改变了这个添加'blogposts = blogposts'后,它开始了这个。重申,当我运行'出口FLASK_APP = app.py'然后'烧瓶运行'在我的本地主机上运行它,它给了我那个错误 – Jshee

0

我有同样的问题。我解决了它作为终端到该文件夹​​的更改目录包含app.py然后运行

export FLASK_APP=app.py 

那么,你应该运行

python -m flask run 
相关问题