title: html forms download file django
body: <p>I am stuck with this issue in Django: I want to download a file already existing on my server through a html form. The file to be downloaded is served through a function in views. My problem is with  html form and passing the file name  to view. How can I pass the name of the file from form toward view without having to select the file?</p>
  
  <pre><code>In html I have: 
  
  # 'content' keeps the name of the file to be downloaded
      {% block content %}
  {{content}}
    &lt;table&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;form action="" method="POST" enctype="multipart/form-data"&gt;
            &lt;input type="file"  name="file"/&gt;
            &lt;br /&gt;
            &lt;input type="submit" value="Download File" /&gt;
          &lt;/form&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  
  {% endblock %}
  </code></pre>
  
  <p>When I select the file and press submit button, it works but I need a different behavior: the name of the file containing results (downloadable file) to be passed to views into a  variable. The views will then serve it.</p>
  
  <pre><code>The view which handled the downloadable file:
  def forecast(request):
  
      if request.method == 'POST':
          #handle_download_file(request.FILES['file'], str(request.FILES['file']))
          print('request.method este: ',request.method)
  
          RESULTS_filename = 'frcst_'+ str(request.FILES['file'])
          #download('GET', RESULTS_filename)
          file_path = os.path.join(os.path.relpath('forecast/'), RESULTS_filename)
          print (file_path)
          print(settings.MEDIA_ROOT)
          with open(file_path,'rb') as fh:
              response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
              response['content-disposition'] = 'attachment; filename='+RESULTS_filename
              print(response)
              return response
      HttpResponseRedirect('/forecast/')
      return render(request,'result_links.html',{'content':'frcst_history.csv'})
  </code></pre>