简体   繁体   English

加载烧瓶应用程序时获取404页面

[英]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 . 当您祁门功夫appviewsviews.pyappapplication 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM