[英]Get 404 page while load flask app
I've just read about flask. 我刚刚读过关于长颈瓶的事情。
http://flask.pocoo.org/docs/0.10/quickstart/ http://flask.pocoo.org/docs/0.10/quickstart/
First tried to write a small app, worked good. 首先尝试编写一个小型应用程序,效果很好。 Then I split app to the files and got 404 empty page.
然后我将应用程序拆分为文件,并得到404空白页。 Could anyone give me an advice.
谁能给我个建议。 Where I was wrong.
我哪里错了。
structure of project: 项目结构:
project/
application/
templates/
main.html
__init__.py
views.py
run.py
file __init__.py
文件
__init__.py
from flask import Flask
app = Flask(__name__)
file run.py
文件
run.py
import os
import sys
from application import app
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, PROJECT_DIR)
if __name__ == '__main__':
app.run(debug=True)
file views.py
文件
views.py
from flask import render_template
from application import app
@app.route("/")
def index():
return render_template("main.html")
In run.py
, you're importing from application import app
which brings in your app
object from __init__.py
. 在
run.py
,您正在from application import app
,该app
从__init__.py
引入了您的app
对象。 Great! 大!
But that's all it does. 但这就是全部。
Your views.py
file gets the same variable from __init__.py
and registers a view. 您的
views.py
文件从__init__.py
获取了相同的变量并注册了一个视图。 This is what you want to import from run.py
: 这就是您要从
run.py
导入的run.py
:
import os
import sys
from application.views import app
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, PROJECT_DIR)
if __name__ == '__main__':
app.run(debug=True)
When you impor app
from views
, views.py
pulls app
from application
. 当您祁门功夫
app
从views
, views.py
拉app
从application
。 Thus, your run.py
gets the app
object, but it comes from views.py
where it had the route registered. 因此,您的
run.py
获取了app
对象,但它来自在其路线已注册的views.py
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.