patternpythonflaskMinor
Flask form validation technique
Viewed 0 times
techniqueformflaskvalidation
Problem
This is currently the code that I'm using to validate and check for errors in a flask app, where the form fields are coming from a /registration html page:
```
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
if request.form['register'] == 'Register':
if request.form.get("eulacheck") != "on":
return regError("Please read and agree to the Terms and Conditions and the License Terms.")
if request.form['username'] == "":
return regError("Please select a username.")
if request.form['password1'] == "":
return regError("Please enter a password.")
if request.form['password2'] == "":
return regError("Please confirm password.")
if request.form['password1'] != request.form['password2']:
return regError("Passwords did not match. Please enter passwords again.")
username = request.form['username']
password = request.form['password1']
hash = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt())
id = randint(1000,9999)
if request.form['email1'] != "":
if request.form['email2'] == "":
return regError("Please confirm email.")
if request.form['email1'] != request.form['email2']:
return regError("Email addresses did not match. Please enter emails again.")
email = request.form['email1']
insert_db("INSERT INTO Users (ID, Username, Email, Hash) VALUES (?, ?, ?, ?)", (id, username, email, hash))
flash("Account registration successfull! Please use your credentials to login below.")
return render_template("login.html",pageType=['login'],flashType="info")
insert_db("INSERT INTO Users (ID, Username, Hash) VALUES (?, ?, ?)", (id, username, hash))
flash("Account registration succes
```
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
if request.form['register'] == 'Register':
if request.form.get("eulacheck") != "on":
return regError("Please read and agree to the Terms and Conditions and the License Terms.")
if request.form['username'] == "":
return regError("Please select a username.")
if request.form['password1'] == "":
return regError("Please enter a password.")
if request.form['password2'] == "":
return regError("Please confirm password.")
if request.form['password1'] != request.form['password2']:
return regError("Passwords did not match. Please enter passwords again.")
username = request.form['username']
password = request.form['password1']
hash = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt())
id = randint(1000,9999)
if request.form['email1'] != "":
if request.form['email2'] == "":
return regError("Please confirm email.")
if request.form['email1'] != request.form['email2']:
return regError("Email addresses did not match. Please enter emails again.")
email = request.form['email1']
insert_db("INSERT INTO Users (ID, Username, Email, Hash) VALUES (?, ?, ?, ?)", (id, username, email, hash))
flash("Account registration successfull! Please use your credentials to login below.")
return render_template("login.html",pageType=['login'],flashType="info")
insert_db("INSERT INTO Users (ID, Username, Hash) VALUES (?, ?, ?)", (id, username, hash))
flash("Account registration succes
Solution
Looking at
This can easily be condensed using
Also, you are not inserting whitespaces between arguments.
Another stylistic issue is some of your lines are too long. For all lines,
For making it look less messy and repetitive, look at this answer to a Stack Overflow question. It is about implementing
register(), the format is as following:def register():
if request.method == "POST":
if request.form['register'] == 'Register':
stuff()
return render_template("register.html",pageType=['register'])This can easily be condensed using
and:def register():
if request.method == "POST" and request.form['register'] == 'Register':
stuff()
return render_template("register.html", pageType=['register'])Also, you are not inserting whitespaces between arguments.
stuff(3,3) # <--Unconventional
stuff(3, 3) # <--More conventionalAnother stylistic issue is some of your lines are too long. For all lines,
line.length <= 79. You can read about the specific python conventions on the Python website.For making it look less messy and repetitive, look at this answer to a Stack Overflow question. It is about implementing
switch and case statements into Python, which seems perfect for your situation.Code Snippets
def register():
if request.method == "POST":
if request.form['register'] == 'Register':
stuff()
return render_template("register.html",pageType=['register'])def register():
if request.method == "POST" and request.form['register'] == 'Register':
stuff()
return render_template("register.html", pageType=['register'])stuff(3,3) # <--Unconventional
stuff(3, 3) # <--More conventionalContext
StackExchange Code Review Q#74884, answer score: 2
Revisions (0)
No revisions yet.