In my Views.py, I try to download the csv file first to client side then only wish to redirect to another page.
Below was my code
def main(request):
...
...
url = '//file1.km.in.com/sd/recipe/' "/" model "_" code ".csv"
filename=model "_" code ".csv"
download_csv(url,filename)
data = {"TableForm": TableForm, "devicelist": devicelist_1}
time.sleep(10)
return redirect('/ProductList/BetaTest', data)
def download_csv(url,filename):
csv = process_file(url)
response = HttpResponse(csv, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=' filename
return response
def process_file(file_handle):
df = pd.read_csv(file_handle, index_col=False)
return df.to_csv(index=False)
However, download function didn't work but it directly redirect to BetaTest page.
I try to edit to below code, and now the download function is work but it cannot redirect to another page:
def main(request):
...
...
data = {"TableForm": TableForm, "devicelist": devicelist_1}
csv = process_file(url)
response = HttpResponse(csv, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=' filename
return response
time.sleep(10)
return redirect('/ProductList/BetaTest', data)
Is there any ideas to overcome this issue?
CodePudding user response:
A return
statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned.
So there is no possibility to call two return statements in a single call.
CodePudding user response:
You can return multiple values by simply returning them separated by commas.
return example1, example2
CodePudding user response:
Instead of using return
, you should use yield
:
def stream_response(request):
def generator():
for x in range(1,11):
yield f"{x}\n"
time.sleep(1)
return StreamingHttpResponse(generator())
CodePudding user response:
Try this: views.py
def main(request):
...
...
data = {"TableForm": TableForm, "devicelist": devicelist_1}
if request.method=='GET':
csv = process_file(url)
response = HttpResponse(csv, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=' filename
return response
return redirect('Url_NAME', data)