2017-04-07 116 views
0

我目前正在測試login()路由,但遇到重定向問題。 我想重定向到主索引頁面。需要幫助瞭解燒瓶url_for()

我的認證藍圖是這樣的:

from flask import Blueprint, render_template, request, redirect, url_for 

mod = Blueprint('auth', __name__, url_prefix='/auth') 


@mod.route('/login', methods=['GET', 'POST']) 
def login(): 
    if request.method == 'POST': 
     return redirect(url_for('general.index')) 
    return render_template('auth/login.html') 

@mod.route('/logout', methods=['GET', 'POST']) 
def logout(): 
    # Delete session token 
    return render_template('auth/logout.html') 

@mod.route('/tokeninfo') 
def token_info(): 
    return render_template('auth/tokeninfo.html') 

此外,我general藍圖的定義是:

from flask import Blueprint, render_template 

mod = Blueprint('general', __name__) 


@mod.route('/') 
@mod.route('/index') 
def index(): 
    return render_template('general/index.html') 

然而,每次我提交表單/login路線我重定向到localhost:5000/auth並且收到404 page not found error

我試圖調整在以下幾個方面的return redirect(url_for('general.index')),但無一成功:

  • return redirect(url_for(general.index))
  • return redirect(url_for('general.index'))
  • return redirect(url_for('../general.index'))

編輯:

看來,提交登錄表單只是一個POST on/auth,而不是/ auth/login。這是什麼出現在服務器日誌上:

127.0.0.1 - - [07/Apr/2017 15:06:59] "GET /auth/login HTTP/1.1" 200 - 
404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. 
127.0.0.1 - - [07/Apr/2017 15:07:03] "POST /auth HTTP/1.1" 404 - 

我在想什麼/不理解? 在此先感謝

回答

0

問題出在模板!

在我登錄的模板,我以前有:

<form action="/auth" method="post" class="form-horizontal"> 

然而,由於我轉換到藍圖供電的應用程序,我需要以下變化:

<form action="/auth/login" method="post" class="form-horizontal"> 

現在重定向工作如預期。