如何从Flask中制作HTML弹出窗口? [英] How can I make a html popup from flask?

查看:2040
本文介绍了如何从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.db.commit()
        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.db.commit()
    flash('Thank you for registering')
    return redirect('/')

这篇关于如何从Flask中制作HTML弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆