2016-07-14 14 views
0

djangoが応答ファイルをダウンロードした後、成功ページにリダイレクトしたいと思います。 私は何をしなければなりませんか?リターンアタッチメントレスポンス後に成功ページにリダイレクト

ファイルは、ユーザが要求するたびにdjangoによって生成されるため、ディレクトリには実際のファイルがありません。

"content"変数は、文字形式の文字列です。

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

ありがとうございました。

+0

何か "有効" のコードブロックの末尾に(self.success_url)は'それを行う必要があります。 – Evert

+1

OPは既に添付ファイルを返信として返信しているため、動作しません。これを行う方法はありません。 2つの応答を返すことはできません。 –

+0

これで代わりに、リターンレスポンスなしでダウンロードファイルを提供するためにこの方法を使用できるものはありますか? –

答えて

0

クライアント側でリダイレクトする必要があります。 `返すHttpResponseRedirectの線に沿って

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> 
+0

ダウンロードが成功した後ではなく、リンクをクリックすると直ちにリダイレクトされます。それが私の質問を理解する方法です。 – BlackJack

関連する問題