'How can I make a html popup from flask?
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('/')
Solution 1:[1]
It's not a pop-up, but you can use the flask's system for 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 log in')
return redirect('/form/')
else:
cur.execute("INSERT INTO users VALUES (?, ?, ?)", [email, username, password])
g.db.commit()
flash('Thank you for registering')
return redirect('/')
Solution 2:[2]
I had the same problem.
This is what I did as a workaround:
- I installed Visual Studio 2019 Community edition on my system. I got my copy through https://visualstudio.microsoft.com/subscriptions/. It may be available at other sites as well.
- Then download and install AJAX toolkit from DevExpress. It will now install properly and include the AJAX toolkit controls in the Visual Studio 2019 toolbox. It WILL NOT include the new controls in the Visual Studio 2022 toolbox.
- To include the new controls in the Visual Studio 2022 toolbox, follow the instructions at add ajaxtoolkit to toolbox
This workaround has worked for me so far. I hope it works for you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RiveN |
| Solution 2 |
