Python 如果request.files中的文件不为空,如何实现

Python 如果request.files中的文件不为空,如何实现,python,flask,flask-wtforms,Python,Flask,Flask Wtforms,我试图实现的逻辑是:如果用户没有将文件上传到Filefield,从而使其为空,那么就不要将文件上传到S3 bucket或创建指向它的链接。第8/9行没有达到我的预期,因为它仍然将空文件上传到bucket,并在我的票据上创建指向它的链接 uploads = [['frontview', 'upload', '<b>Frontview: </b>'],['backview', 'upload2', '<b>Backview: </b>'], ['ele

我试图实现的逻辑是:如果用户没有将文件上传到Filefield,从而使其为空,那么就不要将文件上传到S3 bucket或创建指向它的链接。第8/9行没有达到我的预期,因为它仍然将空文件上传到bucket,并在我的票据上创建指向它的链接

uploads = [['frontview', 'upload', '<b>Frontview: </b>'],['backview', 'upload2', '<b>Backview: </b>'], ['electricalsupply', 'upload3', '<b>Electrical Supply: </b>'], ['fullstructure', 'upload4', '<b>Full Structure: </b>'], ['controlroom', 'upload5', '<b>Control Room: </b>'], ['otherdoc', 'upload6', '<b>Other Documentation: </b>'], ['customersignoff', 'upload7', '<b>Customer Sign-Off: </b>']]
        file_upload_urls = []
        folder = str(datetime.datetime.now()).replace(' ', '') + '/' # sets folder to the current date & time while getting rid of spaces & adding forward slash for linking purposes
        for upload in uploads: #loops through uploads array
            files = request.files.getlist(upload[1])
            section_urls = []
            for file in files: #loops through uploads array more specifically each File (upload, upload2, upload3, etc)
                if file not in request.files:
                    filename = str(upload[0] + '_' + file.filename).replace(' ', '')
                    section_urls.append(current_app.config['NEVCODOCS_BASE_URL'] + folder + filename)
                    path = "ServiceRegistration/" + folder + filename
                    s3.Bucket('nevcodocs').put_object(Key=path, Body=file)
            file_upload_urls.append([upload[2], section_urls])
​
        # Installation photos are placed in an unordered list & formats links of photos for ticket submission
        for section in file_upload_urls:
            ticket_html = ticket_html + section[0] + '<ul>'
            for url in section[1]:
                ticket_html = ticket_html + '<li> <href=' + url + '">' + url + '</href></li>'
            ticket_html = ticket_html + '</ul>'

解决方案:如果不是file.filename==:

请粘贴问题中的所有相关代码。我们不能从截图中复制。很抱歉。我以为放在垃圾桶里会更干净。我现在已经用代码段更新了这篇文章,请适当地格式化它,以便它可以在Python中实际执行。@StevenNguyen看起来更干净对堆栈溢出的重要性不如永远可用。如果dpaste进入或清除其旧数据等,则此问题不再是未来查看者的有用参考。如果您循环查找文件中的文件:,如果文件不在文件中:将始终为False,因为您刚刚从同一列表中获取了该文件。
        for file in files: #loops through uploads array more specifically each FileField (upload, upload2, upload3, etc)
            # if not len(request.files[file]) == 0:
            if file not in files:
                filename = str(upload[0] + '_' + file.filename).replace(' ', '')
                section_urls.append(current_app.config['NEVCODOCS_BASE_URL'] + folder + filename)
                path = "ServiceRegistration/" + folder + filename
                s3.Bucket('nevcodocs').put_object(Key=path, Body=file)