返回附件响应后重定向到成功页面

时间:2016-07-14 10:45:46

标签: python django

我想通过响应在django服务下载文件后重定向到成功页面。 我该怎么办?

每次用户请求时都会由django生成文件,因此目录中没有真实文件。

“content”变量只是.ical格式的字符串。

def index(request):
    if request.method == 'POST':
        form = UploadText(request.POST)
        if form.is_valid():
            data = convert2calendar(form.cleaned_data['regHtml'])
            open_day = form.cleaned_data['open_date_semester']
            end_day = form.cleaned_data['end_date_semester']

            content = create_ical_download(open_day, end_day, data)

            response = HttpResponse(content_type='text/ics')
            response['Content-Disposition'] = 'attachment; filename="export.ics"'
            response.write(content)
            return response
    else:
        form = UploadText()
    return render(request, 'genclass/index.html', {'form': form})

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

你应该通过在客户端重定向来实现它!

JS代码

document.getElementById('link-with-redirect').addEventListener('click', function() {
  setTimeout(function() {
    // Should be triggered after download started
    document.location.href='{% url "redirect_destination" %}';
  });
 }, false);

HTML代码

<a href="{% url 'file_to_download' %}" id="link-with-redirect">Click here to download Django 1.9</a>
相关问题