Flask默认运行request.method'POST'而不是'GET'

时间:2018-05-06 00:43:16

标签: python http post flask get

我正在开发这个Flask应用程序,我按下“登录”按钮,我被重定向到“仪表板”。

这是我的默认路线的简化代码,它打开登录页面:

@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')

login.html有一个form action="/" method="POST"的表单会在上面的默认路由('/')中触发条件elif request.method == 'POST'

这是路由的简化代码('/ 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')

这就是问题所在。仪表板路由在登录后运行POST方法虽然(根据我的概念)它应该运行GET方法(因为GET是默认的)。

它给出的输出是“它是POST”。请帮忙。谢谢:))

2 个答案:

答案 0 :(得分:3)

您不能只是致电return dashboard()。您必须启动重定向 - 告诉浏览器加载/dashboard

return flask.redirect(flask.url_for('dashboard'))

答案 1 :(得分:1)

您需要更改应用程序的结构。不是登录用户的两条路线,显示登录页面,显示仪表板,并提供用户验证,考虑四条路线:1)归属路线,显示带有href的HTML 2)登录路线,接受用户输入,重定向到3)验证路由,其将用户提交到会话,以及4),用户从验证路由重定向的仪表板路由。最后,不是在每个路径的主体中进行用户验证,而是创建一个装饰器:

home.html

<html>
  <body>
     <h1>Welcome to the application</h1>
     {%if not_validated%}
       <a href='/login'>Login</a>
     {%endif%}
  </body>
</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>

dashboard.html

<html>
  <body>
     <h1>Welcome, @{{username}}</h1>
  </body>
 </html>

接下来,您需要创建路线。首先,创建一个包装器来验证用户:

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'])