2015-01-21 54 views
0

我想分開我的應用程序與燒瓶使用藍圖,但我得到AssertionError雖然沒有同名的功能。我想如果函數名稱不同,默認端點也會不同。我已經找到它了,但我仍然很努力。請幫忙;(不能解決我的燒瓶bluprint斷言錯誤

這是我controllers.py

import flask 
from flask import render_template, request, redirect, url_for, flash, make_response, g, session, jsonify 
from apps import app, db 
from apps.models import * 

from apps.controllers2 import app2 

#Set application.debug=true to enable tracebacks on Beanstalk log output. 
#Make sure to remove this line before deploying to production. 
app.debug=True 
app.register_blueprint(app2) 

@app.route('/', methods=['GET', 'POST']) 
@app.route('/intro', methods=['GET', 'POST']) 
def hmp_showIntro(): 
    return render_template('intro.html') 


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

這種分離器,controller2.py

from flask import render_template, request, redirect, url_for, flash, make_response, g, session, jsonify 
from flask import Blueprint 
from apps import app, db 
app2 = Blueprint('app2', __name__) 

@app2.route('/main') 
def hmp_showMain(): 
    return render_template('main.html') 

,這裏是我所得到

mod_wsgi (pid=15429): Exception occurred processing WSGI script '/opt/python/current/app/apps/controllers.py'. 
Traceback (most recent call last): 
    File "/opt/python/current/app/apps/controllers.py", line 17, in <module> 
    @app.route('/intro', methods=['GET', 'POST']) 
    File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1013, in decorator 
    self.add_url_rule(rule, endpoint, f, **options) 
    File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 62, in wrapper_func 
    return f(self, *args, **kwargs) 
    File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 984, in add_url_rule 
    'existing endpoint function: %s' % endpoint) 
AssertionError: View function mapping is overwriting an existing endpoint function: hmp_showIntro 
+1

類似'url_for'的函數使用您的視圖函數的名稱來解析URL。這就是爲什麼不能有兩個具有相同名稱'hmp_showIntro'的視圖函數。無關緊要,它們在單獨的藍圖中,因爲藍圖只是簡化視圖的註冊,直到調用'app.register_blueprint',以便與在主應用程序對象上註冊具有相同名稱的函數基本相同。你可以通過使用[route' decorator]的''endpoint'參數來避免這種情況(http://flask.pocoo.org/docs/0.10/api/#flask.Flask.route)。 – boreq 2015-01-21 18:09:35

回答

0

您的應用程序已有路徑,可以使用hmp_showIntro名稱查看該功能的項目。您可以執行controllers.py的雙重導入。

+0

謝謝。我無法找到雙重進口的地方,但問題解決了 – HMPark 2015-01-23 15:19:47

+0

問題解決了很好,但如何? – 2015-05-15 09:32:32