Flask is overwriting webpage

When I make a user in HTML type in two inputs it will either return "wrong" or return "Your info is " + first_name +" "+ last_name. But it keeps overwriting everything to display it how do I make it display on the current page?

Section of the code

def index():
    if request.method == "POST":
       # getting input with name = fname in HTML form
       first_name = request.form.get("fname")
       # getting input with name = lname in HTML form
       last_name = request.form.get("lname")
       if first_name != "Admin" or last_name != "12345":
        return "WRONG"
        return "Your info is " + first_name +" "+ last_name
    return render_template("index.html")


<form action="{{ url_for("index")}}" method="post">
            <label for="firstname">First Name:</label>
            <input type="text" id="firstname" name="fname" placeholder="firstname">
            <label for="lastname">Last Name:</label>
            <input type="text" id="lastname" name="lname" placeholder="lastname">
            <button type="submit">Login</button>

One typically renders a new page or template with the posted data. You’re just returning "WRONG" or the user entered data, without rendering a template with those values and returning the HTML like you do in the return render_template("index.html"). Your provided template also doesn’t have any places to put the variables like {{last_name}} or whatever the syntax actually is. The typical form workflow is a GET to display the form (possibly with previously entered but invalid data asking the user to correct the values) and a POST to validate and do stuff (usually redirecting to some other page).

Further, you’re likely to get better assistance posting a repl. There is a flask project template on repl.it. Much easier to debug a live project.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.