OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I clear a login error message upon reloading the page in python flask?

  • Thread starter Thread starter user25533071
  • Start date Start date
U

user25533071

Guest
I am working on creating a login system where the computer checks if the email and psw typed in by the user exists in the user database, and if so the user gets sent to the dashboard route. However, if the user does not exist than an error should show up on the screen saying that the email or password is incorrect. Ive got the error to render correctly by passing in the warning message to an html template, but for some reason when the page is reloaded the error is still their even though the fields are empty.

Backend code for checking if user exists, and if so then send user to dashboard, but if user does not exist show warning text:

Code:
@app.route("/", methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        email = request.form.get("email")
        psw = request.form.get("password")
        users = fetch_users()
        if check_user_exist(email, psw):
            return render_template('dashboard.html', users=users)
        
        return render_template('login.html', warning = "Email or Password is incorrect")
    

    return render_template('login.html', warning = "")

html code:

Code:
{% if warning %}
        <div class="warning">
            {{ warning }}
        </div>
        {% endif %}

I have already tried to use session variables by saying session.pop('warning', None) when the error message is not needed, and using session['warning'] = "Email already registered. Please log in." when i need to display the error message and passing this through the html template but the same issue persists( error doesnt go away after reloading)

Backend code using sessions:

Code:
@app.route("/", methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        email = request.form.get("email")
        psw = request.form.get("password")
        users = fetch_users()
        if check_user_exist(email, psw):
            # Clear the warning message from the session
            session.pop('warning', None)
            return render_template('dashboard.html', users=users)
        
        # Set the warning message in the session
        session['warning'] = "Email or Password is incorrect"
        return render_template('login.html')

    return render_template('login.html')

Frontend code:

Code:
{% if session.warning %}
<div class="warning" role="alert">
    {{ session.warning }}
</div>
{% endif %}
<p>I am working on creating a login system where the computer checks if the email and psw typed in by the user exists in the user database, and if so the user gets sent to the dashboard route. However, if the user does not exist than an error should show up on the screen saying that the email or password is incorrect. Ive got the error to render correctly by passing in the warning message to an html template, but for some reason when the page is reloaded the error is still their even though the fields are empty.</p>
<p>Backend code for checking if user exists, and if so then send user to dashboard, but if user does not exist show warning text:</p>
<pre><code>@app.route("/", methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form.get("email")
psw = request.form.get("password")
users = fetch_users()
if check_user_exist(email, psw):
return render_template('dashboard.html', users=users)

return render_template('login.html', warning = "Email or Password is incorrect")


return render_template('login.html', warning = "")

</code></pre>
<p>html code:</p>
<pre><code>{% if warning %}
<div class="warning">
{{ warning }}
</div>
{% endif %}
</code></pre>
<p>I have already tried to use session variables by saying session.pop('warning', None) when the error message is not needed, and using session['warning'] = "Email already registered. Please log in." when i need to display the error message and passing this through the html template but the same issue persists( error doesnt go away after reloading)</p>
<p>Backend code using sessions:</p>
<pre><code>@app.route("/", methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form.get("email")
psw = request.form.get("password")
users = fetch_users()
if check_user_exist(email, psw):
# Clear the warning message from the session
session.pop('warning', None)
return render_template('dashboard.html', users=users)

# Set the warning message in the session
session['warning'] = "Email or Password is incorrect"
return render_template('login.html')

return render_template('login.html')
</code></pre>
<p>Frontend code:</p>
<pre><code>{% if session.warning %}
<div class="warning" role="alert">
{{ session.warning }}
</div>
{% endif %}
</code></pre>
 

Latest posts

Top