Skip to content
Advertisement

I don’t know why while sending the variable to HTML it is showing UnboundLocalError. I tried similar Stackoverflow solution but it didn’t solve

Error

I don’t know why while sending the variable to HTML it is showing UnboundLocalError: local variable ‘prdct’ referenced before assignment.

When I don’t pass any values from render_template() method then the code is running fine. Also the output that I’m printing using print(prdct) is also working fine.

Following is my code:

Code part-1: python file

Code part-2: HTML

Advertisement

Answer

Your code does not create prdct in all cases when executing the function. Only when you are entering this with request.Method = "POST" it will work.

You cannot return something unconditionally that may not have been created:

def test():
    if False:
        k = 42

    return k


test()

will throw the a different error your code – the reasons are similar.

The if condition is not met, k = 42 is never created – so you cannot return it in all possible cases.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement