Answer the question
In order to leave comments, you need to log in
Why does the function not see the variable?
def actions():
global flash
node_name = request.args.get('node_name')
if node_name:
form = AddForm(obj = get_node_values(node_name))
check = True
return render_template('actions.html', form = form)
form = AddForm()
if request.method == 'POST' and form.validate_on_submit():
operator = form.operator_name.data.lower()
db_values = models.node(operator_name = operator, node_name = node,\
node_id = nod_id)
current_page = request.form['current_page']
if check:
flash(change_value_in_db(db_value))
return redirect(url_for('actions'))
if check
, an exception occurs:local variable 'check' referenced before assignment
. I don’t understand why it can occur, because the variable is check
declared and processed in one function (there is a suspicion that this situation is due to branching, so Google is for help, but if anyone here knows, please tell me) Answer the question
In order to leave comments, you need to log in
global is bad practice
and no CODE is "even worse"
def actions():
global flash
node_name = request.args.get('node_name')
if node_name:
form = AddForm(obj = get_node_values(node_name))
check = True
return render_template('actions.html', form = form)
form = AddForm()
if request.method == 'POST' and form.validate_on_submit():
operator = form.operator_name.data.lower()
db_values = models.node(operator_name = operator, node_name = node,\
node_id = nod_id)
current_page = request.form['current_page']
if check:
flash(change_value_in_db(db_value))
return redirect(url_for('actions'))
following good advice:
def actions():
global flash
node_name = request.args.get('node_name')
if node_name:
form = AddForm(obj = get_node_values(node_name))
check = True
return render_template('actions.html', form = form)
form = AddForm()
if request.method == 'POST' and form.validate_on_submit():
operator = form.operator_name.data.lower()
node = form.node_name.data.lower()
nod_id = form.node_id.data.lower()
db_values = models.node(operator_name = operator, node_name = node,\
node_id = nod_id)
current_page = request.form['current_page']
if check:
flash(change_value_in_db(db_value))
return redirect(url_for('actions'))
flash(add_value_in_db(db_values))
return redirect(url_for('actions'))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question