I build a social media website here I am making a signup form with some errors flashing on unsuccessful signup. Python :
@app.route("/signup-request", methods=['POST'])
def signupRequest():
errors = ["","",""]
# Error 0 => username taken
# Error 1 => email used
# Error 2 => password mismatch
name = request.form["name"]
usrnm = request.form["username"]
email = request.form["email"]
paswd = request.form["password"]
cpswd = request.form["confirm password"]
RacF = open(".\\database\\accounts.csv","rt")
read = csv.reader(RacF)
for rows in read:
if rows[1] == usrnm:
errors[0] = "Username has already been taken"
if rows[2] == email:
errors[1] = "Email has already been registered"
if paswd != cpswd:
error[2] = "Password and confirm password doesn't match"
RacF.close()
if errors != ["","",""]:
flash(errors)
print("signup failed")
print(errors)
return redirect(request.referrer)
else:
WacF = open(".\\database\\accounts.csv","at")
WacF.write(f'{name},{usrnm},{email},{paswd}\n')
WacF.close()
session["user"] = usrnm
return "<h1>You're registered</h1>"
HTML:
<form action="/signup-request" method="POST" class="form shadow">
{% with message = get_flashed_messages() %}
<p>
<label for="nm">Name :</label><br/>
<input type="text" name="name" id="nm" placeholder="Full Name..." class="wd-lg" required/><br/>
<label for="usrnm">Username :</label><br/>
<input type="text" name="username" id="usrnm" placeholder="Username..." class="wd-lg" required/><br/>
{% if message[0] != "" %}
{{ message[0] }}<br/>
{% endif %}
<label for="email">Email Address :</label><br/>
<input type="email" name="email" id="email" placeholder="E-Mail Address" class="wd-lg" required/><br/>
{% if message[1] != "" %}
{{ message[1] }}<br/>
{% endif %}
Password :<br/>
<input type="password" name="password" id="paswd" placeholder="Password" class="wd-lg" required/><br/>
<input type="password" id="cpaswd" name="confirm password" placeholder="Confirm Password" class="wd-lg" required/>
{% if message[2] != "" %}
{{ message[2] }}<br/>
{% endif %}
</p>
<p>
<input type="checkbox" id="chk" onclick="showPassword()"/>
<label for="chk" style="cursor: pointer;">Show Password</label>
</p>
<p>
<input type="submit" value="Signup" class="btn wd-lg btn-primary"/>
</p>
{% endwith %}
</form>
But I am facing an error:
File "D:\My Coding\Websites\Cloud Town\signup.py", line 31, in signupRequest error[2] = "Password and confirm password doesn't match" TypeError: 'type' object does not support item assignment.
Please let me know hot to solve this. I will be thankful to you.
You misspelled errors
as error
:
Change the line error[2] = "Password and confirm password doesn't match"
to errors[2] = "Password and confirm password doesn't match"
@app.route("/signup-request", methods=['POST'])
def signupRequest():
errors = ["","",""]
# Error 0 => username taken
# Error 1 => email used
# Error 2 => password mismatch
name = request.form["name"]
usrnm = request.form["username"]
email = request.form["email"]
paswd = request.form["password"]
cpswd = request.form["confirm password"]
RacF = open(".\\database\\accounts.csv","rt")
read = csv.reader(RacF)
for rows in read:
if rows[1] == usrnm:
errors[0] = "Username has already been taken"
if rows[2] == email:
errors[1] = "Email has already been registered"
if paswd != cpswd:
errors[2] = "Password and confirm password doesn't match" # Added the 's' in errors
RacF.close()
if errors != ["","",""]:
flash(errors)
print("signup failed")
print(errors)
return redirect(request.referrer)
else:
WacF = open(".\\database\\accounts.csv","at")
WacF.write(f'{name},{usrnm},{email},{paswd}\n')
WacF.close()
session["user"] = usrnm
return "<h1>You're registered</h1>"