flask:如何用多个应用程序来构建项目?

时间:2023-01-27 10:44:36

Lets say I want to build a project Facebook

假设我想建立一个项目Facebook

I need a project structure like

我需要一个项目结构

facebook/
         __init__.py
         feed/
             __init__.py
             models.py
             business.py
             views.py
         chat/
             __init__.py
             models.py
             business.py
             views.py
         games/
             __init__.py
             models.py
             business.py
             views.py
         common/
             common.py

         runserver.py

How can I structure this well so that when I run

我怎样才能把它组织得很好,当我跑的时候

python facebook/runserver.py

It loads views from all my apps internally?
I want to keep this structure because extending the project further is more natural way

它会在内部加载我所有应用程序的视图吗?我想保留这个结构,因为进一步扩展项目是更自然的方式

I am trying to follow their advice, but don't really understand where I need to write

我试着听从他们的建议,但真的不明白我该写些什么

from flask import Flask

app = Flask(__name__)

and how to import all views from all apps at one place, please help

以及如何从一个地方导入所有应用的所有视图,请帮忙

If lets say I write the above code in facebook/__init__.py, then how in facebook/feed/views.py, I can do

如果我在facebook/__init__写了上面的代码。py,然后在facebook/feed/视图中。py,我能做的

from facebook import app

2 个解决方案

#1


11  

Use blueprints. Each one of your sub-applications should be a blueprint, and you load every one of them inside your main init file.

使用蓝图。每个子应用程序都应该是一个蓝图,并在主init文件中加载每个子应用程序。

Answering your second question

回答你的第二个问题

from flask import Flask
app = Flask(__name__)

You should put this into facebook/__init__.py

你应该把这个放到facebook/__init__.py上

BTW, my runserver.py and settings.py always resides one level under facebook/.

顺便说一句,我的runserver。py和设置。py总是位于facebook/下的一个级别。

Like this:

是这样的:

facebook/
         __init__.py
         feed/
             __init__.py
             models.py
             business.py
             views.py
         chat/
             __init__.py
             models.py
             business.py
             views.py
         games/
             __init__.py
             models.py
             business.py
             views.py
         common/
             common.py

runserver.py
settings.py

Content of runserver.py:

runserver.py内容:

from facebook import app
app.run()

I suppose the content of settings.py should not be explained.

我想是设置的内容。py不应该被解释。

Content of facebook/__init__.py:

facebook的内容/ __init__ . py:

from flask import Flask
app = Flask(__name__)
app.config.from_object('settings')
from blog.views import blog #blog is blueprint, I prefer to init them inside views.py file
app.register_blueprint(blog,url_prefix="/blog")

#2


8  

I have tried blueprints and came up with a solution which works for me, let me know if you have other ideas.

我试过设计图,想出了一个适合我的解决方案,如果你有其他想法,请告诉我。

Project Structure

项目结构

facebook/
        runserver.py
        feed/
            __init__.py
            views.py
        chat/
            __init__.py
            views.py

Code

代码

# create blueprint in feed/__init__.py
from flask import Blueprint

feed = Blueprint('feed', __name__)
import views

# create blueprint in chat/__init__.py
from flask import Blueprint

chat = Blueprint('chat', __name__)
import views

# add views (endpoints) in feed/views.py
from . import feed

@feed.route('/feed')
def feed():
    return 'feed'

# add views (endpoints) in chat/views.py
from . import chat

@chat.route('/chat')
def chat():
    return 'chat'

# register blueprint and start flask app
from flask import Flask
from expense import expense
from budget import budget

app = Flask(__name__)
app.register_blueprint(feed)
app.register_blueprint(chat)
app.run(debug=True)

In Action

在行动

 * Running on http://127.0.0.1:5000/
# Hit Urls
http://127.0.0.1:5000/feed # output feed
http://127.0.0.1:5000/chat # output chat

#1


11  

Use blueprints. Each one of your sub-applications should be a blueprint, and you load every one of them inside your main init file.

使用蓝图。每个子应用程序都应该是一个蓝图,并在主init文件中加载每个子应用程序。

Answering your second question

回答你的第二个问题

from flask import Flask
app = Flask(__name__)

You should put this into facebook/__init__.py

你应该把这个放到facebook/__init__.py上

BTW, my runserver.py and settings.py always resides one level under facebook/.

顺便说一句,我的runserver。py和设置。py总是位于facebook/下的一个级别。

Like this:

是这样的:

facebook/
         __init__.py
         feed/
             __init__.py
             models.py
             business.py
             views.py
         chat/
             __init__.py
             models.py
             business.py
             views.py
         games/
             __init__.py
             models.py
             business.py
             views.py
         common/
             common.py

runserver.py
settings.py

Content of runserver.py:

runserver.py内容:

from facebook import app
app.run()

I suppose the content of settings.py should not be explained.

我想是设置的内容。py不应该被解释。

Content of facebook/__init__.py:

facebook的内容/ __init__ . py:

from flask import Flask
app = Flask(__name__)
app.config.from_object('settings')
from blog.views import blog #blog is blueprint, I prefer to init them inside views.py file
app.register_blueprint(blog,url_prefix="/blog")

#2


8  

I have tried blueprints and came up with a solution which works for me, let me know if you have other ideas.

我试过设计图,想出了一个适合我的解决方案,如果你有其他想法,请告诉我。

Project Structure

项目结构

facebook/
        runserver.py
        feed/
            __init__.py
            views.py
        chat/
            __init__.py
            views.py

Code

代码

# create blueprint in feed/__init__.py
from flask import Blueprint

feed = Blueprint('feed', __name__)
import views

# create blueprint in chat/__init__.py
from flask import Blueprint

chat = Blueprint('chat', __name__)
import views

# add views (endpoints) in feed/views.py
from . import feed

@feed.route('/feed')
def feed():
    return 'feed'

# add views (endpoints) in chat/views.py
from . import chat

@chat.route('/chat')
def chat():
    return 'chat'

# register blueprint and start flask app
from flask import Flask
from expense import expense
from budget import budget

app = Flask(__name__)
app.register_blueprint(feed)
app.register_blueprint(chat)
app.run(debug=True)

In Action

在行动

 * Running on http://127.0.0.1:5000/
# Hit Urls
http://127.0.0.1:5000/feed # output feed
http://127.0.0.1:5000/chat # output chat