2017-01-19 45 views
0

我發現了一個問題瓶端點問題 - 裝飾登錄檢查

BuildError: Could not build url for endpoint 'home'. Did you mean 'Welcome' instead? 

歡迎的另一個功能在應用程序(第一個函數定義)

def loginRequired(func): 
    def wrapper(): 
     access_token = session.get('access_token') 
     if access_token is None: 
      return redirect(url_for('login')) 
     else: 
      return func 

    return wrapper 
@app.route('/Homepage') 
@loginRequired 
def home():  
    return render_template('Homepage.html', title="Title") 

@app.route('/login') 
def login(): 
    callback = url_for('authorized', _external=True) 
    return google.authorize(callback=callback) 


@app.route(REDIRECT_URI) 
@google.authorized_handler 
def authorized(resp): 
    access_token = resp['access_token'] 
    session['access_token'] = access_token, '' 
    return redirect(url_for('home')) 


@google.tokengetter 
def get_access_token(): 
    return session.get('access_token') 

回溯消息:

Traceback (most recent call last): 
    File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__ 
    return self.wsgi_app(environ, start_response) 
    File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app 
    response = self.make_response(self.handle_exception(e)) 
    File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception 
    reraise(exc_type, exc_value, tb) 
    File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app 
    response = self.full_dispatch_request() 
    File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request 
    rv = self.handle_user_exception(e) 
    File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception 
    reraise(exc_type, exc_value, tb) 
    File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request 
    rv = self.dispatch_request() 
    File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request 
    return self.view_functions[rule.endpoint](**req.view_args) 
    File "C:\Python27\lib\site-packages\flask_oauth.py", line 433, in decorated 
    return f(*((data,) + args), **kwargs) 
    File "C:\Users\Barry\PycharmProjects\FYP\welcome.py", line 169, in authorized 
    return redirect(url_for('home')) 
    File "C:\Python27\lib\site-packages\flask\helpers.py", line 312, in url_for 
    return appctx.app.handle_url_build_error(error, endpoint, values) 
    File "C:\Python27\lib\site-packages\flask\app.py", line 1641, in handle_url_build_error 
    reraise(exc_type, exc_value, tb) 
    File "C:\Python27\lib\site-packages\flask\helpers.py", line 305, in url_for 
    force_external=external) 
    File "C:\Python27\lib\site-packages\werkzeug\routing.py", line 1758, in build 
    raise BuildError(endpoint, values, method, self) 
BuildError: Could not build url for endpoint 'home'. Did you mean 'Welcome' instead? 

谷歌正在做好的跡象(被提示),但我似乎無法得到重定向工作家。 如果我回家更改爲另一個功能(即isent需要登錄),然後重定向工作

另外,如果我把線

access_token = session.get('access_token') 
if access_token is None: 
    return redirect(url_for('login')) 

家庭功能內,不包括裝飾它的工作原理

思考?

+0

你可以發佈跟蹤回錯誤信息嗎? –

+0

感謝您的回覆。 Traceback在這裏http://pastebin.com/wZS0s6Xm – bpb101

回答

1

找到了解決方案

我跟着Flasks文檔,發現這個登錄裝飾。這工作。 看來你使用functools.wraps()來處理這個給你。 http://flask.pocoo.org/docs/0.12/patterns/viewdecorators/

from functools import wraps 
    from flask import g, request, redirect, url_for 

    def login_required(f): 
    @wraps(f) 
    def decorated_function(*args, **kwargs): 
     access_token = session.get('access_token') 
     if access_token is None: 
      return redirect(url_for('login', next=request.url)) 
     return f(*args, **kwargs) 
    return decorated_function