N
N
nano_e_t_42015-11-16 23:57:57
Python
nano_e_t_4, 2015-11-16 23:57:57

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'))

When trying to execute the condition block if check, an exception occurs:
local variable 'check' referenced before assignment
. I don’t understand why it can occur, because the variable is checkdeclared 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)
Thank you

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nirvimel, 2015-11-17
@nirvimel

checkdeclared only if if node_name:otherwise remains undeclared.

D
Dimonchik, 2015-11-17
@dimonchik2013

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'))

N
nano_e_t_4, 2015-11-17
@nano_e_t_4

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 question

Ask a Question

731 491 924 answers to any question