简体   繁体   中英

Login app using python flask not redirecting to login page

I am creating a login app using python flask and mongodb at the backend, everything is working fine but in login its not redirecting into login page.

#here is my code
@app.route('/login/', methods=["GET","POST"])
def login_page():
    error = ''
    try:
       conn = connection()
       collection=MongoClient()["blog"]["users"]
       if request.method == "POST":

        data = collection.find({"username":(request.form['username'])})
        #data1=collection.find({"password":(request.form['password'])})
        if sha256_crypt.verify(request.form.password,data):
            print("password verification suceessful")
            session['logged_in'] = True
            session['username'] = request.form['username']

            flash("You are now logged in")
            return redirect(url_for("dashboard"))

        else:
            error = "Invalid credentials, try again."

    gc.collect()

    return render_template("login.html", error=error)

except Exception as e:

    error = "Invalid credentials, try again."
    return render_template("login.html", error = error)

Here, I am trying to verify the user-entered username and password is matching with my database collection. If matching, the user can login else throw one error messgae and redirect to login page. Can anyone help me out to solve the issue?

here is the answer for my question,

@app.route('/login/', methods=["GET","POST"])
def login_page():
    error = ''
    try:
        conn = connection()
        collection=MongoClient()["blog"]["users"]
        if request.method == "POST":
            #collection = MongoClient()["blog"]["users"]

            data = collection.find_one({"username":(request.form['username'])})
            #data1=collection.find({"password":(request.form['password'])}
            #data = c.fetchone()[2]
            password=request.form['password']
            if sha256_crypt.verify(password,data['password']):
                print("password verification suceessful")
                session['logged_in'] = True
                session['username'] = request.form['username']

                flash("You are now logged in")
                return redirect(url_for("dashboard"))

            else:
                error = "Invalid credentials, try again."

       # gc.collect()

        return render_template("login.html", error=error)

    except Exception as e:
        #flash(e)
        error = "Invalid credentials, try again."
        return render_template("login.html", error = error)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM