Answer the question
In order to leave comments, you need to log in
Why doesn't the function inside @app.route work?
Hello, I'm trying to make the code detect the presence / absence of the required parameters (at least one capital letter in the login and one number) in the login and give an error if something is wrong. Everything jupyter notebook
works fine in Flask, but for some reason the function does not work in Flask, although I don’t teach errors. Only the first condition (ALL GOOD / NOT GOOD) is displayed, but when the condition is not met, additional information (about what exactly is wrong) is not displayed.
Here is what I have in the script:
@app.route('/result')
def result():
username = request.args.get('username')
upper_count = 0
number_count= 0
ok_message=''
reject_message=''
uppercase_err_message=''
num_err_message=''
def result(uppercase, num):
if uppercase == 0:
uppercase_err_message = 'Need an UPPERCASE'
if num == 0:
num_err_message = 'need a NUM'
for i in username:
if i.isupper():
upper_count +=1
elif i.isdigit():
number_count += 1
result(upper_count, number_count)
if upper_count != 0 and number_count !=0:
ok_message = 'ALL GOOD!'
else:
reject_message = 'NOT GOOD...'
return render_template ('result.html',
username=username,
ok_message=ok_message,
reject_message=reject_message,
uppercase_err_message=uppercase_err_message,
num_err_message=num_err_message)
<h1>Your username is {{username}}</h1>
{{ok_message}}
{{reject_message}}
{{upercase_err_message}}
{{num_err_message}}
Answer the question
In order to leave comments, you need to log in
1. Why declare this function inside another? What is its meaning anyway? You can just throw it away, move its block to the left and move it for a cycle.
2. Why give functions the same names?
Let's at least like this, otherwise it looks scary...
@app.route('/result')
def result():
username = request.args.get('username')
ok_message=''
reject_message=''
uppercase_err_message=''
num_err_message=''
has_upper = any([i.isupper() for i in username])
has_digit = any([i.isdigit() for i in username])
if not has_upper:
uppercase_err_message = 'Need an UPPERCASE'
elif not has_digit:
num_err_message = 'need a NUM'
if has_upper and has_digit:
ok_message = 'ALL GOOD!'
else:
reject_message = 'NOT GOOD...'
return render_template(
'result.html',
username=username,
ok_message=ok_message,
reject_message=reject_message,
uppercase_err_message=uppercase_err_message,
num_err_message=num_err_message
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question