如何从Flask中制作HTML弹出窗口?

编程入门 行业动态 更新时间:2024-10-25 00:36:54
本文介绍了如何从Flask中制作HTML弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建一个Web应用程序,并在python上使用了flask,我创建了一个注册系统,用户可以在该系统上登录网站及其详细信息(电子邮件,用户名和密码),并将其保存在sqlite3数据库中.我这样做的目的是,如果用户在注册时输入了用户名或电子邮件,并且该用户名或电子邮件位于数据库中,则会带您返回登录页面并且不保存其数据,我该如何做因此,当它将您重定向回表单页面时,会弹出一条错误消息?

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代码:

<!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>

相关的python代码:

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.dbmit() return redirect('/')

推荐答案

它不是弹出窗口,但是您可以使用烧瓶的消息传递系统.这是"flash"命令.

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

要在您的代码中使用它,它可能看起来像这样: 如果结果: flash('您已经注册,请登录')

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.dbmit() flash('Thank you for registering') return redirect('/')

更多推荐

如何从Flask中制作HTML弹出窗口?

本文发布于:2023-08-06 04:43:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹出窗口   Flask   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!