Flask: using multiple packages in one application.

I am just starting with a flask and I am trapped. I am trying to write a small blog to get used to the structure, so I made two packages: "auth" and "posts". I read the section "Large Applications" in the "Documents checkbox" .

My catalog is as follows.

>/root
>>run.py 

>>/posts

>>>____init____.py  
>>>views.py  
>>>/templates
>>>/static  

>>/auth  
>>>____init____.py  
>>>views.py  
>>>/templates
>>>/static

run.py looks like this:

from flask import Flask
from auth import auth_app
from posts import posts_app

auth_app.run()
posts_app.run()

/posts/__init__.pyand /auth/__init__.pylook like this:

from flask import Flask

auth_app = Flask(__name__)

import auth.views

and view.py look like this:

from auth import auth_app

@auth_app.route('/auth/')
def index():
    return "hello auth!"

But whenever I start the server, only localhost / auth / is available, and everything else gives 404, som, I assume that the posts application does not start.

Can anyone help?

+5
source share
2 answers

auth_app.run() . posts_apps . Flask run(). , Flask .

, , blueprints. (auth posts) . ...

from flask import Flask
from auth import auth_blueprint
from posts import post_blueprint

app = Flask(__name__)
app.register_blueprint(auth_blueprint)
app.register_blueprint(post_blueprint)
app.run()
+5

, Mark , , werkzeug.wsgi.DispatcherMiddleware.

( ), DispatcherMiddleware. , URL.

- frontend backend - URL-, .

, Matt Wright " " Overholt, . : (frontend), API, URL. *:

    from werkzeug.serving import run_simple
    from werkzeug.wsgi import DispatcherMiddleware
    from overholt import api, frontend


    application = DispatcherMiddleware(frontend.create_app(), {
        '/api': api.create_app()
    })


    if __name__ == "__main__":
        run_simple('0.0.0.0', 5000, application, use_reloader=True, use_debugger=True)

, , , .. Python.

* , run_simple() - .

+4

All Articles