title: Downloading simple text file in Django
body: <p>I'm trying to serve a simple text file with Django without success so far. I think that my Django code is OK:</p>
  
  <pre><code>def status(request):
      # Get data from database
      parameters = get_parameters_from_database()
      if request.method == "GET":
          // Stuff here to render the view for a GET request
          return render_to_response('myapp/status.html', {'page_name': 'status', 'parameters' : parameters})
  
      elif request.method == "POST":
          if request.POST['request_name'] == 'download_txt':
              file_path = parameters['file_path']
              with open(file_path, 'rb') as fsock:
                  response = HttpResponse()
                  response['content_type'] = 'text/plain'
                  response['Content-Disposition'] = 'attachment; filename=current.txt'
                  response.write(fsock.read())
                  print(response)
                  return response
  </code></pre>
  
  <p>When the POST request is received, it get the following printed in webserver console, so I think it's ok:</p>
  
  <pre><code>&lt;HttpResponse status_code=200, "text/html; charset=utf-8"&gt;
  </code></pre>
  
  <p>So I think the problema is that I am not handling the success event in Jquery's Ajax method:</p>
  
  <pre><code>$('#downloadButton').click(function(){
      $.ajax({
        url: '',
        method: 'POST',
        data: {
          request_name: 'download_txt'
        },
        success: function (data) {        
          //TODO
        },
        error: function (err) {
          console.log('Error downloading file');
        }
      });
  });
  </code></pre>
  
  <p>The problem is that I have no idea of how I should handle the success event: I thought that the browser would automatically download the file once the server answered with that <code>content_type</code></p>
  
  <p>Any help?</p>