LoginSignup
7
11

More than 5 years have passed since last update.

Flask サイト構築備忘録

Last updated at Posted at 2015-02-22

ディレクトリ構成

基本構成は以下で、appの下の_init_.pyに初期動作を書きこむ。

root/
┣run.py
┣app/
 ┣_init.py
 ┣config.py
 ┣module1/
  ┣__init
_.py
  ┣models.py
  ┣controllers.py
  ┣forms.py
 ┣templates/
 ┣static/

run.py
from app import create_app

if __name__ == "__main__":
    app = create_app()
    app.run()
app/__init__.pyy
from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension
from flask.ext.sqlalchemy import SQLAlchemy # Import SQLAlchemy
from flask.ext.mongoengine import MongoEngine # Import Mongoengine
from flask.ext.admin import Admin
from app import config

# app setup
db = SQLAlchemy()
mongodb = MongoEngine()
admin = Admin()
toolbar = DebugToolbarExtension()

def create_app():

    app = Flask()
    app.config.config_from_object(config)

    #Initializing
    db.init_app(app)
    mongodb.init_app(app)
    admin.init_app(app)
    toolbar.init_app(app)

   # Import a module
    from app.module1.controllers import mod_app

    # Register blueprint(s)
    app.register_blueprint(mod_app)

    # Build the database:
    # This will create the database file using SQLAlchemy
    db.create_all()

    return app

こんな感じでしておけば、各モジュールで、url_prefixを指定しておき、register_bluepointで登録でいける。
configはファイルのままでもOKだが、windowsなどの場合"../"は微妙なので、importしてからオブジェクトで読ますのがいいと思う。
templatesやsataticはどこでもいいが、appの下がスマート。

7
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
11