当信息不正确时,用户提交时如何将内容保留在表单中,这样他们就不必在上方键入所有内容

问题描述 投票:1回答:1

我是编码和学习的新手。我构建了一个flask应用程序,并具有一个自定义注册和登录表单。

我正在寻找一种方法来在用户单击提交时将内容保留在注册表单或登录表单中-但是仅当他们输入的信息引起错误消息时[用户名存在,密码不匹配,等等。]因此他们可以看到他们的错误并进行更正。

我不确定是否要问适当的问题,但是搜索多个地方,似乎找不到正确的解决方案。我敢肯定这很简单

我的注册和登录表单在满足所有条件时工作正常,但显然在显示错误消息时-表单清除...

您能指导我如何解决或将我指向职位吗?

这是我的注册表格:

<h1> Register</h1>
            
            <form method="POST" action="/signup">
                <input type="text" name="name" class="input-box" placeholder="Name"  required >
                <input type="text" name="username" class="input-box" placeholder="Username" required>
                <input type="email" name="email" class="input-box" placeholder="Email" required>
                <!-- Having issues with the pattern feature... Though it shows when you hover, is doesn't show when you type the pattern incorrectly-->
                <input type="Password" name="password" class="input-box" placeholder="Password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z).{8,}" title="Must contain: at least one number, one upper and lowercase letter, and at least 8 Characters" required>
                <!-- Having issues with the pattern feature... Though it shows when you hover, is doesn't show when you type the pattern incorrectly-->
                <input type="password" name="conf_password" class="input-box" placeholder="Confirm Password" id="conf_password" pattern="(?=.\*d)(?=.*[a-z])(?=.*[A-Z).{8,}" title="Must contain: at least one number, one upper and lowercase letter, and at least 8 Characters" required>
                
                <p>
                    <span><input type="checkbox"></span>
                    I agree to the terms of the services
                </p>
                <div class="msg">{{ msg }}</div>
                <button type="submit" class="signup-btn">Register</button>
                <hr>
                <p class="or">OR</p>
                <button type="button" class="twitter-btn">Login with Twitter</button>
                <p>Do you have and account? <a href="login">Sign In</a></p>
            </form>

这是应用程序的路径:

#Add Register Route
@app.route('/signup', methods=['GET', 'POST'])
def signup():
    msg=''
    if request.method == "POST" :
        details = request.form
        Name = details['name']
        Username = details['username']
        Email = details['email']
        Password = details['password']
        # Can do SHA encryption : 
        # Conf_password = sha256_crypt.encrypt(details['conf_password'])
        Conf_password = details['conf_password']

        # Check if account exists using MySQL
        cursor = mysql.connection.cursor()
        
        # Check DB for username...
        result = cursor.execute("SELECT * FROM users WHERE username = %s", [Username])
        account = cursor.fetchone()

        if not Username and not Password and not Email:
            error = 'Please fill out the form!'
            return render_template('signup.html', msg=error)
        elif account:
            error = 'Username already exists'
            return render_template('signup.html', msg=error)
        elif not re.match(r'[A-Za-z0-9]+', Username):
            error = 'Username must contain only characters and numbers!'
            return render_template('signup.html', msg=error)
        elif not re.match(r'[^@]+@[^@]+\.[^@]+', Email):
            error = 'Invalid email address!'
            return render_template('signup.html', msg=error)
        elif not re.match(r'[A-Za-z0-9]+', Password):
            error = 'Password must contain only characters and numbers!'
            return render_template('signup.html', msg=error)
        if Password != Conf_password:
            error = 'Passwords do not MATCH!'
            return render_template('signup.html', msg=error)

我做错了什么?

'谢谢您,如果之前多次问过同样的问题,我深表歉意。

DL

javascript html forms flask registration
1个回答
1
投票

[这是我做的一个简单示例,您现在可以看到,在第一次电子邮件为空的情况下,即使提交了signup.html文件,即使提交了表单,它也将输入返回到页面,请注意,只是一个例子,我没有做任何验证

your_script.py

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/signup', methods=['GET', 'POST'])
def sign_up():
  email = ""
  if request.method == "POST":
    email = request.form["email"]
  return render_template("signup.html", email=email)

signup.html

<!DOCTYPE html>
<html lang="en-us">
<head>
  <title></title>
</head>
<body>

  <form action="/signup" method="post">
    <input type="email" name="email" value="{{email}}">
    <input type="submit" value="submit">
  </form>

</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.