How can I make a html popup from flask?

余生长醉 提交于 2020-05-13 14:42:54

问题


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('/')

回答1:


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('/')


来源:https://stackoverflow.com/questions/45586304/how-can-i-make-a-html-popup-from-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!