Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在/uploaded_files/local variable';i';作业前参考?_Python_Django - Fatal编程技术网

Python 在/uploaded_files/local variable';i';作业前参考?

Python 在/uploaded_files/local variable';i';作业前参考?,python,django,Python,Django,我有这样一个代码: choices = request.POST.getlist('choice') #choices1 = len(choices) for i in choices: new_source = source +"/"+ i start_date = datetime.datetime.utcnow().replace(tzinfo=utc) source12 = new_source.replace(' ',

我有这样一个代码:

choices = request.POST.getlist('choice')
    #choices1 = len(choices) 
    for i in choices:
        new_source = source +"/"+ i 
        start_date = datetime.datetime.utcnow().replace(tzinfo=utc)
        source12 = new_source.replace(' ', '') #Remove whitespaces
        subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', source12, destination], 
                                    stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
        end_date = datetime.datetime.utcnow().replace(tzinfo=utc)
        duration = end_date - start_date
        start_date = start_date.strftime("%B %d, %Y, %H:%M%p")
        end_date = end_date.strftime("%B %d, %Y, %H:%M%p")
        #basename = os.path.basename(source) #Get file_name
        extension = os.path.splitext(i)[1][1:] #Get the file_extension
        fullname = os.path.join(destination, i) #Get the file_full_size to calculate size
        st = int(os.path.getsize(fullname))
        f_size = size(st, system=alternative)
        dur1 = convert_timedelta(duration)
        dur = duration_conversion(dur1)
        b = File(users_id=request.user.id, file_name=i, type=extension, source='ngs.pradhi.com', start_date=start_date, end_date=end_date, duration=dur, size_overview=st, size=f_size, flag='F')
        b.save()

    return render_to_response('uploaded_files.html', {'files': b, 'username':username, 'host':a, 'files_server':files_in_server, 'file_size':filesize, 'date':date, 'total_files_selected':i , 'list_users':users_b}, context_instance=RequestContext(request))  
我想找到选择列表的长度,以便向用户显示。首先,我尝试将变量传递为:

choices = request.POST.getlist('choice')
choices1 = len(choices) 
当用户选择了多个文件时,将choices1变量传递给模板仅返回0。因此,我尝试传递变量I,该变量会产生以下错误:

UnboundLocalError at /uploaded_files/ local variable 'i' referenced before assignment?
我做错了什么

 i = ""

 choices = request.POST.getlist('choice[]')
 for i in choices:
    ........

在分配之前,我应该有默认值i=“”,要获得列表,您必须将[]

'total\u files\u selected':i
应该是
'total\u files\u selected':len(choices)
是,我这样做了,但它只返回0,因为您显然在
选项
字段中没有任何内容-这也解释了为什么
I
最初未定义:它从未进入for循环,原因完全相同。也许您应该打印
request.POST的值。