找不到python烧瓶404

时间:2019-04-17 03:18:45

标签: python flask

我不熟悉烧瓶开发,因此无法进行以下应用程序工作。我希望在run.py中声明Flask应用程序,并将其导入其他包中,例如下面的myapp。我之所以这样设计,是因为有多个独立的应用程序/程序包。尝试访问网址“ ... / hello”时收到404。我哪里错了?谢谢!

项目布局如下:

├── myapp
│   ├── __init__.py
│   └── myapp.py
└── run.py

myapp / __ init__.py包含:

from flask import Flask
my_app = Flask(__name__)
from . import myapp

myapp / myapp.py包含:

from run import app
@app.route('/hello')
def show_conf2():
    return "Hello, world!"

run.py包含:

from flask import Flask
from gevent.pywsgi import WSGIServer
import myapp
@app.route('/')
def index():
    return 'Home route.'
app = Flask(__name__)
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

2 个答案:

答案 0 :(得分:1)

您已声明Flask的两个实例,一个在__init__.py中,另一个在run.py中。

Flask官方文档提供了有关how to breakdown a larger application in multiple modules的教程。

我已经更新了您的代码,文件名和文件夹结构。

文件夹结构:

├── application
│   ├── __init__.py
│   └── views.py
├── requirements.txt
└── run_application.py

run_application.py

from flask import Flask
from gevent.pywsgi import WSGIServer
from application import app

http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

application/__init__.py

from flask import Flask
app = Flask(__name__)

import application.views

application/views.py

from application import app

@app.route('/')
def index():
    return 'Home route.'

@app.route('/hello')
def hello():
    return 'Hello World!'

requirements.txt

Click==7.0
Flask==1.0.2
gevent==1.4.0
greenlet==0.4.15
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
Werkzeug==0.15.2

运行命令:

python run_application.py

输出

  1. 本地路线http://localhost:5000/

Home route

  1. 你好路线http://localhost:5000/hello

hello route

更新

根据Flask documentation on Blueprints

  

Flask使用蓝图的概念来制作应用程序组件   并支持应用程序内或跨应用程序的通用模式   应用程序。

更新了代码以方便blueprints

更新的目录结构:

├── application1
│   ├── __init__.py
│   └── routes.py
├── application2
│   ├── __init__.py
│   └── routes.py
├── apps.py
└── requirements.txt

apps.py

from flask import Flask
from gevent.pywsgi import WSGIServer
from application1.routes import application1_blueprint
from application2.routes import application2_blueprint


app = Flask(__name__)
app.register_blueprint(application1_blueprint)
app.register_blueprint(application2_blueprint)    

http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

application1/__init__.py:空白文件

application1/routes.py

from flask import Blueprint

application1_blueprint = Blueprint('application1', __name__)

@application1_blueprint.route('/app1')
def application1_index():
    return 'Home route for application1'

@application1_blueprint.route('/hello1')
def hello1():
    return 'Hello World from application1!'

application2/__init__.py:空白文件

application2/routes.py

from flask import Blueprint

application2_blueprint = Blueprint('application2', __name__)

@application2_blueprint.route('/app2')
def application2_index():
    return 'Home route for application2'

@application2_blueprint.route('/hello2')
def hello2():
    return 'Hello World from application2!'

输出:

来自/app1

application1路线:

application 1 view

来自/app2

application2路线:

application 2 view

答案 1 :(得分:0)

在您的代码中,我想您在flask应用程序中执行了run.py。在这种情况下,您的应用程序不会定义任何路由规则。只需修改您的代码并将其放入单个文件中,它将是可行的

from flask import Flask
from gevent.pywsgi import WSGIServer
#import myapp

app = Flask(__name__)

@app.route('/hello')
def show_conf2():
    return "Hello, world!"

http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()