How can I make a html popup from flask?

14,370

It's not a pop up, but you can use the flask system of messaging. it's the "flash" command.

to use this in your code, it might look like this: if result: flash('You are already registered, please login')

    return redirect('/form/')

else:
    cur.execute("INSERT INTO users VALUES (?, ?, ?)", [email, username, password])
    g.db.commit()
    flash('Thank you for registering')
    return redirect('/')
Share:
14,370

Related videos on Youtube

Daniel
Author by

Daniel

Updated on May 25, 2022

Comments

  • Daniel
    Daniel almost 2 years

    I am creating a web application and am using flask on python along with it, I have created a registration system where the user can sign to the website and their details (email, username and password) which are saved in a sqlite3 database. I have made it so if the user enters a username or email, when registering, and that username or email is in the database it takes you back to the sign in page and does not save their data, How can I make it so when it redirects you back to the form page a popup occurs with an error message?

    Html code:

    <!DOCTYPE html>
    <html class='signIn'>
    {% extends "layout.html" %}
    {% block content %}
    
    <body>
    
    <center>
    <form name='myForm' method="post" action="{{ url_for('signup') }}" style='text-align: center;'>
      <p>Email</p>
      <input type="email" name="email" placeholder='Enter you email adress' required oninvalid="setCustomValidity('Please enter a valid email ')" onchange="try{setCustomValidity('')}catch(e){}"></input>
      <p>Username</p>
      <input type="text" name="user" placeholder='Enter your username' required></input>
      <p>Password</p>
      <input type="password" name="password" placeholder='Enter your password' required></input>
      <br><br>
      <input type="submit" value="Signup"></input>
       <a href="{{ url_for('home') }}" class='button_link'>Cancel</a>
    </form>
    </center>
    
    </body>
    {% endblock %}
    </html>
    

    Relevant python code:

    def signup():
        cur = g.db.cursor()
        email = request.form['email']
        username = request.form['user']
        password = request.form['password']
        cur.execute("""SELECT email,username FROM users WHERE email=? OR username=?""",(email, username))
        result = cur.fetchone()
        if result:
    
            return redirect('/form/')
    
        else:
            cur.execute("INSERT INTO users VALUES (?, ?, ?)", [email, username, password])
            g.db.commit()
            return redirect('/')