I am exporting a CSV file on the frontend as the user clicks on the download button, but sometimes I get an empty file and sometimes I get the exact file that I want. This is happening only on the deployment of the app.
The function which is doing all this is :(the channel_list contains a list of dictionaries which has 'url' as key)
def exportfile(request): response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachement; filename="channelUrls.csv"' writer = csv.writer(response) for list in channel_list: writer.writerow([list['url']]) channel_list.clear() return responseThe function which is inserting data to channel_list is :
channel_list = []def home(request): context = {'message' : ""} if request.method == 'POST': rows = [] video_id = [] file = request.FILES["file"].readlines() for f in file: rows.append((f.decode('utf-8'))) for row in rows[0:len(rows)-1]: video_id.append((row[-13:])) video_id.append((rows[len(rows)-1][-11:])) video_id = [x.replace("\r\n","") for x in video_id] search_url = 'https://www.googleapis.com/youtube/v3/videos' parameter = {'key' : settings.YOUTUBE_DATA_API_KEY,'part' : 'snippet','id' : ','.join(video_id) } data = requests.get(search_url,params=parameter) results = data.json()['items'] temp_list = [] for result in results: data = {'name' : result['snippet']['channelTitle'],'url' : f'https://www.youtube.com/channel/{ result["snippet"]["channelId"] }' } temp_list.append(data) [channel_list.append(x) for x in temp_list if x not in channel_list] context['message'] = "Click on Download File to download the file" return render(request,'index.html',context)Complete code of views.py file :
from urllib import responsefrom django.shortcuts import render,HttpResponsefrom django.conf import settingsimport requestsimport csvchannel_list = []def home(request): context = {'message' : ""} if request.method == 'POST': rows = [] video_id = [] file = request.FILES["file"].readlines() for f in file: rows.append((f.decode('utf-8'))) for row in rows[0:len(rows)-1]: video_id.append((row[-13:])) video_id.append((rows[len(rows)-1][-11:])) video_id = [x.replace("\r\n","") for x in video_id] search_url = 'https://www.googleapis.com/youtube/v3/videos' parameter = {'key' : settings.YOUTUBE_DATA_API_KEY,'part' : 'snippet','id' : ','.join(video_id) } data = requests.get(search_url,params=parameter) results = data.json()['items'] temp_list = [] for result in results: data = {'name' : result['snippet']['channelTitle'],'url' : f'https://www.youtube.com/channel/{ result["snippet"]["channelId"] }' } temp_list.append(data) [channel_list.append(x) for x in temp_list if x not in channel_list] context['message'] = "Click on Download File to download the file" return render(request,'index.html',context)def exportfile(request): response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachement; filename="channelUrls.csv"' writer = csv.writer(response) for list in channel_list: writer.writerow([list['url']]) channel_list.clear() return response