python - Importing Views -
my app layout
my_app __init__.py my_app __init__.py startup create_app.py create_users.py common_settings.py core models.py views.py
inner __init__.py
from flask import flask flask_script import manager flask_sqlalchemy import sqlalchemy app = flask(__name__) # wsgi compliant web application object db = sqlalchemy(app) # setup flask-sqlalchemy manager = manager(app) # setup flask-script my_app.startup.create_app import create_app create_app()
create_app.py
from native_linguist_server import app, db @app.before_first_request def initialize_app_on_first_request(): """ create users , roles tables on first http request """ .create_users import create_users create_users() def create_app(extra_config_settings={}): app.config.from_envvar('env_settings_file') # load blueprints manager commands, models , views my_app import core return app
when run app , try load view
in browser, 404
error.
however if change:
from my_app import core
to
from my_app.core import views
it works fine.
could please explain me difference between these 2 calls? have thought from my_app import core
import views.py
, hence there wouldn't issue.
thank you.
from my_app import core
will load , execute my_app/core/__init__.py
(if exists). have access identifiers defined inside or imported __init__.py
.
from my_app.core import views
will load , execute my_app/core/views.py
. you'll have access identifiers defined inside or imported views.py
.
to behavior you're expecting, you'll need import views
inside __init__.py
:
from . import views
Comments
Post a Comment