title: how to serve the downloadable image files in django
body: <p>I have all the images stored at some folder like media in my project, and i have a button in html template that should download the respective image.</p>
  
  <p>Below are my codes</p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>@login_required
  def download_image(request, product_id):
      import os
      # current_site = get_current_site(request)
      product_image =  Product.objects.get(id=product_id)
      product_image_url = product_image.image_code_url()
      print product_image_url, "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;"
      response = HttpResponse(mimetype='application/force-download')
      response['X-Sendfile'] = smart_str(product_image_url)
      response['Content-Length'] = os.stat(product_image_url).st_size
  
      return response  
  </code></pre>
  
  <p><strong>template.html</strong></p>
  
  <pre><code>&lt;input type=button&gt;&lt;a href="{% url 'download_image' product_id%}"&gt;&lt;/a&gt;/&gt;
  </code></pre>
  
  <p><strong>result</strong> : </p>
  
  <pre><code>/media/productb7ab/product792f2764314f40b8bd3b3d58290765cc/codes/Image_1.png  &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
  ERROR 2013-10-25 18:46:21,192 (base) (7258, -1248855232): Internal Server Error: /download/code/88/
  Traceback (most recent call last):
    .......
    .......
    OSError: [Errno 2] No such file or directory: '/media/productb7ab/product792f2764314f40b8bd3b3d58290765cc/codes/Image_1.png'
  </code></pre>
  
  <p>But when i hit the url like <code>localhost:8000/media/productb7ab/product792f2764314f40b8bd3b3d58290765cc/codes/Image_1.png</code> , i can able to view the image, </p>
  
  <p>so finally how to download an image from the filesystem in django ?</p>
  
  <p>What am i doing wrong in my above code ? </p>
  
  <h1>Edit</h1>
  
  <p>After reading the suggested link shared by <code>Brandon</code></p>
  
  <p>I have modified my above method to below code and still the same errors i am facing</p>
  
  <p>from django.core.servers.basehttp import FileWrapper</p>
  
  <pre><code>@login_required
  def downloadimage(request, product_id):
      import os
      # current_site = get_current_site(request)
      image_code =  Product.objects.get(object_id=product_id)
      image_code_url = image_code.image_code_url()
      print image_code_url,"&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;"
      wrapper = FileWrapper(file(image_code_url))
      response = HttpResponse(wrapper, content_type='text/plain')
      response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(image_code_url)
      response['Content-Length'] = os.path.getsize(image_code_url)
      return response
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <pre><code>Error:IOError at /download/code/88/
  [Errno 2] No such file or directory: '/media/productrbab/product792f2764314f40b8bd3b3d58290765cc/codes/Image_1.png'
  Request Method: GET
  Request URL:    http://localhost:8000/download/qrcode/88/
  Django Version: 1.5.4
  Exception Type: IOError
  Exception Value:    
  [Errno 2] No such file or directory: '/media/productrbab/product792f2764314f40b8bd3b3d58290765cc/codes/Image_1.png'
  </code></pre>