Home > Software design >  local variable 'params' referenced before assignment in django
local variable 'params' referenced before assignment in django

Time:09-30

Here's my view

def ATFinfo(Request):
    # inputdata = Request.GET.get('inputdata')
    url = 'www.google.com'
    req = Request.GET
    print("hi",req)
    req_list = list(dict(req).values())
    print("list",req_list)
    params_count = len(req_list)
    print('params_count', params_count)
    if params_count > 0:
        for i in range(params_count):
            params = params   req_list[i 1][0]   '='   req_list[i 1][1]   '&' 
        url = url   params

        print('paramfinal',params)
    return render(Request, 'hello/ATF_Dashboard.html')

In this view i'm getting error local variable 'params' referenced before assignment in line params = params req_list[i 1][0] '=' req_list[i 1][1] '&' How to solve that issue? Also i'm not able to understand what is the problem here

CodePudding user response:

When you tried to use params here specifically the expression params req_list..., the variable params isn't defined yet.

for i in range(params_count):
    params = params   req_list[i 1][0]   '='   req_list[i 1][1]   '&' 

You have to define it first:

params = ""
for i in range(params_count):
    params = params   req_list[i 1][0]   '='   req_list[i 1][1]   '&'

You could also use = operator:

params  = req_list[i 1][0]   '='   req_list[i 1][1]   '&'

Also based on the pasted code, take note that this line:

url = url   params

Doesn't add anything in the logic as url isn't used after that (unless the pasted code is not the complete version).

  • Related