python - Require help understanding Flask url_for() -
i testing login()
route have trouble redirect. redirected main index page.
my authentication blueprint looks this:
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')
additionally, general
blueprint defined as:
from flask import blueprint, render_template mod = blueprint('general', __name__) @mod.route('/') @mod.route('/index') def index(): return render_template('general/index.html')
however, every time submit form on /login
route redirected localhost:5000/auth
, receive 404 page not found error
.
i have tried adjusting return redirect(url_for('general.index'))
in following ways, none successful:
return redirect(url_for(general.index))
return redirect(url_for('general.index'))
return redirect(url_for('../general.index'))
edit:
it seems submitting login form post on /auth, , not on /auth/login. appears on server log:
127.0.0.1 - - [07/apr/2017 15:06:59] "get /auth/login http/1.1" 200 - 404 not found: requested url not found on server. if entered url manually please check spelling , try again. 127.0.0.1 - - [07/apr/2017 15:07:03] "post /auth http/1.1" 404 -
what missing / not understanding? in advance
the issue template!
in login template, had:
<form action="/auth" method="post" class="form-horizontal">
however, since transitioning blueprint powered app, needed following change:
<form action="/auth/login" method="post" class="form-horizontal">
now redirect working expected.
Comments
Post a Comment