[英]Flask running request.method 'POST' by default instead of 'GET'
I am developing this Flask application where I press 'Login' button and I am redirected to "Dashboard". 我正在开发这个Flask应用程序,在其中按下“登录”按钮,然后将我重定向到“仪表板”。
Here's a simplified code for my default route which opens login page: 这是打开登录页面的默认路由的简化代码:
@app.route('/', methods=['POST', 'GET'])
def home():
if not session.get('logged_in'):
if request.method == 'GET':
return render_template('login.html')
elif request.method == 'POST':
if # Username and Password are correct from the login form
session['logged_in'] = True
return dashboard()
else:
return render_template('login.html', message = "Wrong username or password")
else:
return render_template('dashboard.html')
The login.html has a form with form action="/" method="POST"
which triggers the condition elif request.method == 'POST'
in default route('/') above login.html具有形式为form action="/" method="POST"
的表单,该form action="/" method="POST"
触发条件elif request.method == 'POST'
在上面的默认路由('/')中
Here's simplified code for route('/dashboard') 这是route('/ dashboard')的简化代码
@app.route('/dashboard', methods=['POST', 'GET'])
def dashboard():
if session.get('logged_in'):
if request.method == 'GET':
return "it was GET"
elif request.method == 'POST':
return "it was POST"
else:
return render_template('login.html')
Here comes the problem. 问题来了。 The dashboard route runs POST method after login although (according to my concept) it should run GET method (as GET is default). 仪表板路由在登录后运行POST方法,尽管(根据我的概念)它应该运行GET方法(因为GET是默认方法)。
The output it gives is "it was POST". 它给出的输出是“ it was POST”。 Please help. 请帮忙。 Thanks :) 谢谢 :)
You cannot just call return dashboard()
. 您不能只调用return dashboard()
。 You must initiate redirect - tell the browser to load /dashboard
: 您必须启动重定向-告诉浏览器加载/dashboard
:
return flask.redirect(flask.url_for('dashboard'))
You need to change the structure of your application. 您需要更改应用程序的结构。 Instead of two routes that login the user, display the login page, display the dashboard, and provide user validation, consider four routes: 1) the home route, which displays HTML with an href to 2) the login route, which accepts the user input, redirects to 3) a validation route, which commits the user to the session, and 4), the dashboard route which the user is redirected from the validation route. 代替两条用于登录用户的路由,而是显示登录页面,显示仪表板并提供用户验证,请考虑以下四种路由:1)主页路由,显示带有href的HTML到2)登录路由,接受用户输入,将其重定向到3)验证路由,该验证路由将用户提交到会话,以及4),仪表板路由,该用户将仪表板路由从验证路由重定向。 Lastly, instead of user validation in the body of each route, create a decorator: 最后,创建一个装饰器,而不是在每个路径的主体中进行用户验证:
In home.html
: 在home.html
:
<html>
<body>
<h1>Welcome to the application</h1>
{%if not_validated%}
<a href='/login'>Login</a>
{%endif%}
</body>
</html>
In login.html
: 在login.html
:
<html>
<body>
<form action='/user_login' method='POST'>
<input type='email' name='userEmail' placeholder='enter email'>
<input type='password' name='userPassword' placeholder='password'>
<button type='submit'>Login</button>
</form>
</body>
</html>
In dashboard.html
: 在dashboard.html
:
<html>
<body>
<h1>Welcome, @{{username}}</h1>
</body>
</html>
Next, you need to create your routes. 接下来,您需要创建路线。 First, create a wrapper to validate the user: 首先,创建包装器以验证用户:
import functools
import flask
app = flask.Flask(__name__)
def isloggedin(to_Login = False):
def outer(f):
@functools.wraps(f)
def wrapper(*args):
if (flask.session.get('loggedin', False) and not to_Login) or to_Login:
return f(*args)
return flask.redirect('/')
return wrapper
return outer
@app.route('/', methods=['GET'])
def home():
flask.session['loggedin'] = False
return flask.render_template('home.html', not_validated=flask.session.get('loggedin'))
@app.route('/login', methods=['GET'])
@isloggedin(to_Login = True)
def login():
return flask.render_template('login.html')
@app.route('/user_login', methods=['POST'])
def login_user():
email = flask.request.form['userEmail']
password = flask.request.form['userPassword']
flask.session['email'] = email
flask.session['password'] = password
flask.session['loggedin'] = True
flask.redirect('/dashboard')
@app.route('/dashboard')
@isloggedin()
def dashboard():
return flask.render_template('dashboard', username=flask.session['email'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.