0

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?

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any statement i can replace the return response?
@ShiJieTio you can return redirect from backend and use frontend JS to make the download for the user.
0

You can return multiple values by simply returning them separated by commas.

return example1, example2 

5 Comments

hi , it doesn't work, I have try before, the output is 'tuple' object has no attribute 'status_code'
i have try return{response, redirect('/ProductList/BetaTest', data)} also cannot
Link Do check this out.
I still cant get it, based on my case the two function is different category and the variable is passing around, I hope u will help me more
Hmm, I will update it once I find a proper solution you are looking for.
0

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())

2 Comments

I already replace the return to yield but I get "It returned an unawaited coroutine instead. You may need to add an 'await' into your view."
It should do nothing with coroutine, could you briefly describe the changes?
0

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)

2 Comments

Hi , the download function is work but i dunno why the redirect function didn't work at all (return redirect('/ProductList/BetaTest', data))
I think You need to give the name of the path you specified for this in urls.py , i.e. return redirect('name for Url',data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.