title: Serve raw files as text/plain on Django server
body: <p>I have a Django App that's used to upload KML files along with a <code>state</code> value in a model <code>KmlModel</code>. To display these KMLs, I want to access them as raw files on the server which I'm trying to do as follows:</p>
  
  <pre><code>def result(request): #mapped to /getFiles url
      state = request.GET['state']
      res = KmlModel.objects.get(state__state__exact=state)
      obj = res.Kml_File.url
      serve_file(obj)
  
  def serve_file(filename):
      response = HttpResponse(mimetype="text/plain")
      for line in open(filename):
          response.write(line)
      return response
  </code></pre>
  
  <p>The problem I'm facing is, that on trying to access <code>/getFiles?state=SomeState</code>, it gives this error
  <code>OSError at /getFiles
  [Errno 22] Invalid argument: 'http://127.0.0.1:8000/static/img/dynamic/RR.kml'</code> where <code>RR.kml</code> is the matching KML file for <code>SomeState</code>. </p>
  
  <p>Any idea how I can solve this problem?</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>def result(request):
      state = request.GET['state']
      res = KmlModel.objects.get(state__state__exact=state)
      return HttpResponse(res.Kml_File.open(), mimetype='text/plain')
  </code></pre>
  
  <p>Models.py</p>
  
  <pre><code>class KmlModel(models.Model):
      id = models.AutoField(primary_key=True)
      Kml_File = models.FileField(upload_to='dynamic/')
      state = models.ForeignKey(State)
  
      def __unicode__(self):
          return self.state.state
  </code></pre>