2013-06-03 44 views
4

我來自意大利麪代碼PHP背景。我試圖通過使用Flask和MongoDB切入Python來學習MVC。我認爲這個問題可能適用於其他情況。這更多是一個新手Python問題。但這是我第一次使用這種設置進入它的地方。在Python中實現MVC? (使用燒瓶和MongoKit)

我正在使用Flask with Blueprints來佈局我的應用程序。我將每個主要網站功能分解到myapp(模塊/藍圖)的子目錄中。這裏是我的目錄結構

迪爾結構

/proj/config.py 
/proj/runserver.py 
/proj/myapp/ 
/proj/myapp/__init__.py 
/proj/myapp/static/ 
/proj/myapp/templates/ 
/proj/myapp/templates/users/ 
/proj/myapp/templates/forums/ 
/proj/myapp/templates/frontend/ 
/proj/myapp/users/ 
/proj/myapp/users/__init__.py 
/proj/myapp/users/models.py 
/proj/myapp/users/views.py 
/proj/myapp/forums/ .. 
/proj/myapp/frontend/ .. 

所以我想實現這個簡單的例子MongoKit。但不是在一個文件中。我需要將它分散到MVC模式中。

MongoKit樣品

from flask import Flask, request, render_template, redirect, url_for 
from flask.ext.mongokit import MongoKit, Document 

app = Flask(__name__) 

class User(Document): 
    __collection__ = 'user' 
    structure = { 
     'name': unicode, 
     'email': unicode, 
    } 
    required_fields = ['name', 'email'] 
    use_dot_notation = True 

db = MongoKit(app) 
db.register([User]) 

我的應用程序的主要部分是在初始化的.py,它看起來像:

/MYAPP/_ 初始化 _.py

from flask import Flask, render_template, abort 
from flask.ext.mongokit import MongoKit, Document 

from .home.views import mod as home_blueprint 
from .users.views import mod as user_blueprint 
from .forums.views import mod as forum_blueprint 

def create_app(): 
    app = Flask(__name__) 
    app.config.from_object('config') 

    # Register blueprints 
    app.register_blueprint(home_blueprint) 
    app.register_blueprint(user_blueprint, url_prefix="/users") 
    app.register_blueprint(forum_blueprint, url_prefix="/forums") 

    db = MongoKit(app) 

    @app.errorhandler(404) 
    def not_found(error): 
     return render_template('404.html') 

    @app.errorhandler(500) 
    def internal_error(exception): 
     return "Some internal error has taken place. Alert somebody!" 

    return app 

然後我不確定如何處理剩下的事情。我在下面的/myapp/users/models.py中設置了一個類。我知道最後一個陳述沒有定義。我不知道它是否會去那裏,或者如果我需要把它放在其他地方。或者如果它確實去那裏,我如何從init .py中的create_app()獲得「db」。我認爲這與MongoKit和基本Python的東西有關。

/myapp/users/models.py

from flask.ext.mongokit import MongoKit, Document 

class User(Document): 
    structure = { 
    'name': unicode, 
    'email': unicode, 
} 
    use_dot_notation = True 

db.register([User]) 

回答

3

如果你想使用create_app功能,那麼我會改變你的__init__.py來是這樣的:

/myapp/__init__.py

from flask import Flask, render_template, abort 
from flask.ext.mongokit import MongoKit, Document 

from .home.views import mod as home_blueprint 
from .users.views import mod as user_blueprint 
from .forums.views import mod as forum_blueprint 

def create_app(): 
    app = Flask(__name__) 
    app.config.from_object('config') 

    # Register blueprints 
    app.register_blueprint(home_blueprint) 
    app.register_blueprint(user_blueprint, url_prefix="/users") 
    app.register_blueprint(forum_blueprint, url_prefix="/forums") 

    db = MongoKit(app) 

    @app.errorhandler(404) 
    def not_found(error): 
     return render_template('404.html') 

    @app.errorhandler(500) 
    def internal_error(exception): 
     return "Some internal error has taken place. Alert somebody!" 

    return app,db 

# Make sure you are calling create_app func below: 
(app,db) = create_app() 

現在在您的模型代碼中,您可以參考到db變量。

/myapp/users/models.py

from flask.ext.mongokit import MongoKit, Document 

# import db below like this 
from myapp import db 

class User(Document): 
    structure = { 
    'name': unicode, 
    'email': unicode, 
} 
use_dot_notation = True 

# ok to do this below 
db.register([User])