【问题标题】:Python, Flask: Importing functions and variables inside blueprintPython、Flask:在蓝图中导入函数和变量
【发布时间】:2018-04-04 03:15:41
【问题描述】:

如何分别从 app/__init__.pyapp/blueprint/__init__.py 导入函数和变量,在 app/blueprint/views.py 内?

app/__init__.py

def main():
    <..>

app/blueprint/__init__.py

from flask import Blueprint
blueprint = Blueprint('blueprint', __name__, template_folder='templates')

app/blueprint/views.py

import blueprint
import main

【问题讨论】:

    标签: python flask


    【解决方案1】:
    from app.__init__ import *
    from app.blueprint.__init__ import *
    

    应该从两个文件中导入所有函数和变量。

    但是,尽管我认为 init 文件不应该用于此目的。

    下面的 Flask 蓝图示例我使用了我的项目,从 Udemy 教程中学习了结构,我认为这个想法通常是使用 init 文件将 Python 目录制作成一个包,以便您可以在其中导入内容。您可能会更好地使用要导入的函数(不太常见的变量)创建新文件,也许专家会确认,但我认为通常您将 Python 初始化文件留空,除非您真的知道自己在做什么。

    from flask import Flask, render_template
    from Source.common.database import Database
    
    from Source.models.users.views import user_blueprint
    from Source.models.street_lists.views import street_list_blueprint
    # from Source.models.street_reports.views import street_report_blueprint
    
    __author__ = "Will Croxford, with some base structure elements based on Github: jslvtr, \
    from a different tutorial web application for online price scraping"
    
    app = Flask(__name__)
    app.config.from_object('Source.config')
    app.secret_key = "123"
    
    app.register_blueprint(user_blueprint, url_prefix="/users")
    app.register_blueprint(street_list_blueprint, url_prefix="/streetlists")
    # app.register_blueprint(street_report_blueprint, url_prefix="/streetreports")
    
    
    @app.before_first_request
    def init_db():
        Database.initialize()
    
    
    @app.route('/')
    def home():
        return render_template('home.jinja2')
    
    
    @app.route('/about_popup.jinja2')
    def info_popup():
        return render_template('about_popup.jinja2')
    

    Flask 视图文件示例:

    # In this model, views.py files are the Flask Blueprint for this object.
    # ie they describe what HTTP API endpoints are associated to objects of this class.
    
    from flask import Blueprint, render_template, request, redirect, url_for
    
    from Source.models.street_lists.street_list import StreetList
    
    __author__ = 'jslvtr'
    
    
    street_list_blueprint = Blueprint('street_lists', __name__)
    
    
    @street_list_blueprint.route('/')
    def index():
        prop_query = StreetList.get_from_mongo(streetpart="bum")
        return render_template('street_lists/street_list.jinja2', stores=prop_query)
    

    您可以查看 pocoo.org 烧瓶文档示例,并在其他 SO 问题中搜索我认为的 Flask 蓝图模板示例。祝你好运!

    【讨论】:

    • app/blueprint/views.py 内调用from app.__init__ import main,这是pythonic 吗?另外,app/__init__.py 是我的应用的顶层目录。
    • 其实很抱歉,我刚刚看到你可以将 import 语句放在 init 文件中,参见:mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html
    • Ps 可能不会 * 按规则导入所有内容,请参阅stackoverflow.com/questions/17255737/… cmets 此处的第二个答案以进行解释。我认为您的方法错误,您不会从 init 导入内容文件,但是进入这个文件是可以的。正如这篇博文所解释的,如果您导入到 init 文件中,那么您就可以在本地 Python 模块中使用这些导入的包。
    【解决方案2】:

    我阅读了 Will Croxford 建议的博客,这是我的问题的解决方案:

    app/blueprint/views.py

    from app import main
    from app.blueprint import blueprint
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      相关资源
      最近更新 更多