title: How can I download a temporary file in Django?
body: <p>I'm learning how to serve temporary files in Django, and even after reading the <a href="https://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment" rel="nofollow noreferrer">docs</a> I'm having some trouble finishing it all up. These files are dynamically generated temporarily from user input.</p>
  
  <pre><code>def get_queryset(self):
      gcode = "/home/bradman/Documents/Programming/DjangoWebProjects/3dprinceprod/fullprince/media/uploads/tmp/skull.gcode"
      test_file = open(gcode, 'r')
  
      response = HttpResponse(test_file, content_type='text/plain')
      response['Content-Disposition'] = "attachment; filename=%s.gcode" % title
      print (response)
      return response
  </code></pre>
  
  <p>The code above should place my temporary-gcode file from my server into an HttpResponse object, and the return function should download the file. It even slows down like its downloading, but there is no file once it's finished.</p>
  
  <p><a href="https://stackoverflow.com/questions/1156246/having-django-serve-downloadable-files">This question </a>provided most of the useful info and <a href="https://stackoverflow.com/questions/15790136/django-serving-a-download-file">this one</a> was helpful as well, but I can't get it working and don't know how else to test it. I'm not sure if moving to apache or something is appropriate since I have user permission issues and these files are immediately deleted after downloaded. I've checked that "gcode" is the directory url, and gcode content_type should be text/plain. So basically, how can I make my response actually download?</p>
  
  <p><strong>Edit1</strong></p>
  
  <p>Here is the full code, not just the problematic section.</p>
  
  <pre><code>class SubFileViewSet(viewsets.ModelViewSet):
      queryset = subfiles.objects.all()
      serializer_class = SubFilesSerializer
      permission_classes = (permissions.IsAuthenticatedOrReadOnly,
                            IsOwnerOrReadOnly,)
  
      def perform_create(self, serializer):
          serializer.save(owner=self.request.user)
  
      def get_queryset(self):
          req = self.request
          make = req.query_params.get('make')
          model = req.query_params.get('model')
          plastic = req.query_params.get('plastic')
          quality = req.query_params.get('qual')
          filenum = req.query_params.get('fileid')
          hotend = req.query_params.get('hotendtemp')
          bed = req.query_params.get('bedtemp')
  
  
          if make and model and plastic and quality and filenum:            
  
              filepath = subfiles.objects.values('STL', 'FileTitle').get(fileid = filenum)
  
              path = filepath['STL']
              title =  filepath['FileTitle']    
  
              gcode = TheMagic(
                  make=make, 
                  model=model, 
                  plastic=plastic, 
                  qual=quality, 
                  path=path, 
                  title=title, 
                  hotendtemp=hotend,
                  bedtemp=bed)
  
              test_file = open(gcode, 'r')
              response = HttpResponse(test_file, content_type='text/plain')
              response['Content-Disposition'] = "attachment; filename=%s.gcode" % title
              print (response.content)
              return response
  
          else:
              print ('normal or non complete download')
              return self.queryset
  </code></pre>
  
  <p>I'm using Django Rest Framework and trying to create a dynamic response from my API get request to react to parameters given in the URL. So I send a get request passing variables as parameters, it creates a file based on those variables, and lastly sends back the created file. All of this already works except the actual file download at the end. The get request returns an HTTP 500 error.</p>