Home > OS >  I don't know why while sending the variable to HTML it is showing UnboundLocalError. I tried si
I don't know why while sending the variable to HTML it is showing UnboundLocalError. I tried si

Time:03-06

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

CodePudding user response:

Your issue is that if predict.py, whatever function is in there, gets called with an http request other than POST, the prdct variable is undefined.

You have two options:

  1. Define prdct = None at a higher scope than your POST method condition

  2. Move that specific return statement inside your POST method condition block

CodePudding user response:

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.

  • Related