我正在开发一个简单的烧瓶应用程序,它显示一个带有用户,密码,验证通过和电子邮件的基本表单。我现在的方式,如果没有错误,而不是去我告诉它的页面(welcome.html),它只是回到我的主页(index.html)。代码中有注释引导您完成它。请帮助我一直绞尽脑汁和互联网,并在过去一周尝试了很多东西,我需要这个用于学校,否则我将失败。这是我的申请代码:
from flask import Flask, request, redirect, render_template
import jinja2
import cgi
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route("/", methods=['GET'])
def index():
return render_template("index.html")
#if my form makes a post request at the route / (as defined in the form in the index page), then go to index again, or welcome page
@app.route("/", methods=['POST'])
def welcome():
username = str(request.form.get('username-actual'))
password = str(request.form.get('password-actual'))
verifiedpassword = str(request.form.get('verifedpassword-actual'))
email = str(request.form.get('email-actual'))
#if (not username) or len(username) < 3 or len(username) > 20 or ' ' in username: IGNORE THIS
#If any of the fields have any of these conditions, fill the error message with the message. otherwise, empty
if username == '' or len(username) < 3 or len(username) > 20 or ' ' in username:
usererror = "Invalid username lol"
else:
usererror = ""
if password == '' or len(password) < 3 or len(password) > 20 or ' ' in password:
passworderror = "Invalid password lol"
else:
passworderror = ""
if verifiedpassword != password:
verifiedpassworderror = "Passwords dont match lol"
else:
verifiedpassworderror = ""
if email == '' or len(email) < 3 or len(email) > 20 or ' ' in email or '@' not in email:
emailerror = "Invalid email lol"
else:
emailerror = ""
#if any of the error messages are not empty (meaning there are error messages), render the home page again, with the necessary strings provided for jinja
if len(usererror) > 0 or len(passworderror) > 0 or len(verifiedpassworderror) > 0 or len(emailerror) > 0:
return render_template("index",usererror=usererror,
passworderror=passworderror,
verifiedpassworderror=verifiedpassworderror,
emailerror=emailerror,
username=username,
email=email,)
#otherwise, go to the welcome page, with the necessary username
else:
return render_template("welcome.html",username=username)
app.run()
这里分别是我的两个表单index.html和welcome.html:
<!doctype html>
<html>
<head>
<title>
User Sign-up
</title>
<style>
.error { color:red; }
</style>
</head>
<body>
<h1>Sign-up</h1>
<br>
<form action="/" method="post">
<div>
<label>Username: <input type="text" name="username-actual" value="{{username}}"></label>
<p class="error">{{usererror}}</p>
</div>
<div>
<label>Password: <input type="password" name="password-actual"></label>
<p class="error">{{passworderror}}</p>
</div>
<div>
<label>Verify Password: <input type="password" name="verifiedpassword-actual"></label>
<p class="error">{{verifypassworderror}}</p>
</div>
<div>
<label>Email (optional): <input type="text" name="email-actual" value="{{email}}"></label>
<p class="error">{{emailerror}}</p>
</div>
<div>
<input type="submit" name="sign-up-submit" value="Sign Up!">
</div>
</form>
</body>
</html> <p></p>
以下是我的welcome.html: