flask-TypeError: The view function for 'login' did not return a valid response. The function either returned None or ended without a return statement

Viewed 526

I'm trying to create an admin login page where when a user types the wrong password he is redirected to the same page, but when I try to do that I face this type of error, the code is given below, please tell me how can I fix it since I'm not able to solve this error.

mian.py file:

@app.route("/dashboard", methods=['GET', 'POST'])
def login():
   # if he's already loggoed in
   if "user" in session and session['user'] == params['admin_user']:
      posts = Posts.query.all()
      return render_template("dashboard.html", params=params, posts=posts)
   # if he has to login
   if request.method == 'POST':
      username = request.form.get("uname")
      userpass = request.form.get("upass")
      if username == params['admin_user'] and userpass == params['admin_password']:
          # set the session variable
          session['user'] = username
          posts = Posts.query.all()
          return render_template("dashboard.html", params=params, posts=posts)
   # if is not a admin
   else:
      return render_template('login.html', params=params)
2 Answers
@app.route("/dashboard", methods=['GET', 'POST'])
def login():
   # if he's already loggoed in
   if "user" in session and session['user'] == params['admin_user']:
      posts = Posts.query.all()
      return render_template("dashboard.html", params=params, posts=posts)
   # if he has to login
   if request.method == 'POST':
      username = request.form.get("uname")
      userpass = request.form.get("upass")
      if username == params['admin_user'] and userpass == params['admin_password']:
          # set the session variable
          session['user'] = username
          posts = Posts.query.all()
          return render_template("dashboard.html", params=params, posts=posts)
      # if is not a admin
      else:
          return render_template('login.html', params=params)

I think what you want to do is returning the login template when the user tries to login(via POST method) and the username or password were wrong. Maybe you need to make the last else block have an indent.

Because you not return anything that if used post method. I think you can try to remove the last else. Let the function return login page.

Related