2012-10-12 76 views
3

MongoDB網站上的Tumblelog app不起作用。Tumblelog應用程序與Flask和MongoEngine示例不起作用 - 完整新手

我完全按照這個例子,當我在本地主機上運行它時,出現404錯誤。我在Ubuntu 12.0.4上使用Eclipse Indigo(3.7.2)和pyDev。

我不知道這是否是因爲register_blueprints,我包括在__init__.py

我做了這樣的作爲教程:

from flask import Flask 
from flask.ext.mongoengine import MongoEngine 

app = Flask(__name__) 
app.config["MONGODB_DB"] = "my_tumble_log" 
app.config["SECRET_KEY"] = "KeepThisS3cr3t" 

db = MongoEngine(app) 


if __name__ == '__main__': 
    app.run() 


    def register_blueprints(app): 
    # Prevents circular imports 
     from tumblelog.views import posts 
     app.register_blueprint(posts) 
    register_blueprints(app) 

否則,我也跟着教程完全相同。

回答

4

register_blueprints永遠不會被調用 - app.run會阻止,直到您殺死腳本(此時無需添加路由)。

更改順序,一切都將運行:

def register_blueprints(app): 
# Prevents circular imports 
    from tumblelog.views import posts 
    app.register_blueprint(posts) 

register_blueprints(app) 

if __name__ == '__main__': 
    app.run() 

regist_blueprints實際上並沒有阻止圓形進口 - 模式,以避免循環進口是建立在不同的文件中app,並導入這兩個appblueprint成第三個文件運行一切:

# application.py 
from flask import Flask # etc. 

app = Flask("your_package_name") 

 

# tumblelog/views.py 
from flask import Blueprint, current_app # etc. 

posts = Blueprint("tumblelog") 

@posts.route("/") 
def index(): 
    # use current_app rather than app here 

 

# run_server.py (use the same pattern for .wsgi files) 
from application import app 
from tumblelog.views import posts 

app.register_blueprint(posts) 

if __name__ == "__main__": 
    app.run() 
+0

像你說的我已經改變了順序,似乎現在的工作很好。非常感謝 – user94628