title: Display uploaded files in Django
body: <p>I have a dashboard that displays files you uploaded.  But I can't seem to figure out how to iterate through the files.</p>
  
  <p>Here are my models:</p>
  
  <pre><code>@python_2_unicode_compatible
  class Client(models.Model):
      user = models.OneToOneField(User)
      company = models.CharField(max_length=100)
  
      def __str__(self):
          return self.company
  
      class Meta:
          verbose_name_plural = _("Clients")
          verbose_name = _("Client")
          permissions = (
              ("can_upload", _("Can upload files.")),
              ("can_access_uploads", _("Can access upload dashboard.")),
              ("is_client", _("Is a client.")),
          )
  
  @python_2_unicode_compatible
  class ClientUploads(models.Model):
  
      client = models.OneToOneField(Client)
      #created_at = models.DateTimeField(auto_now_add=True)
  
      def generate_filename(self, filename):
          name = "uploads/%s/%s" % (self.client.company, filename)
          return name
  
      file_upload = models.FileField(upload_to=generate_filename)
  
      def __str__(self):
          return self.client.company
  
      class Meta:
          verbose_name_plural = _("Client Uploads")
          verbose_name = _("Client Upload")
  </code></pre>
  
  <p>Here is my view:</p>
  
  <pre><code>@login_required(login_url='/dashboard-login/')
  def dashboard(request):
      current_user = request.user
      current_client = request.user.client
  
      files = ClientUploads.objects.filter(client=current_client).values('file_upload')
  
      if request.method == 'POST':
          form = UploadFileForm(request.POST, request.FILES)
          if form.is_valid():
              new_file = ClientUploads(client=current_client, file_upload = request.FILES['file_upload'])
              new_file.save()
  
          return HttpResponsePermanentRedirect('/dashboard/')
      else:
          form = UploadFileForm()
  
      data = {'form': form, 'client': current_client, 'files': files}
      return render_to_response('dashboard.html', data, context_instance=RequestContext(request))
  </code></pre>
  
  <p>And here is the template:</p>
  
  <pre><code>{% load i18n %}
  
  &lt;table class="table"&gt;
      &lt;div&gt;
          &lt;p&gt;{{ files }}&lt;/p&gt;
      &lt;/div&gt;
  &lt;tr&gt;
      &lt;th&gt;{% blocktrans %}Filename{% endblocktrans %}&lt;/th&gt;
      &lt;th&gt;{% blocktrans %}Size{% endblocktrans %}&lt;/th&gt;
      &lt;th&gt;{% blocktrans %}Uploaded At{% endblocktrans %}&lt;/th&gt;
  &lt;/tr&gt;
  {% for file in files %}
  &lt;tr&gt;
      &lt;th&gt;{{ file.name }}&lt;/th&gt;
      &lt;th&gt;{{ file.size }}&lt;/th&gt;
      &lt;th&gt;{{ file.url }}&lt;/th&gt;
  &lt;/tr&gt;
  {% endfor %}
  
  &lt;/table&gt;
  </code></pre>
  
  <p>The test div in the template displays [{'file_upload': u'uploads/Company/archive_addin_log_408hdCy.txt'}]  Which is what I uploaded.  So it's working, but I can't figure out how to iterate through all of the uploaded files.  All I see are a bunch of blank lines under the table headings.</p>
  
  <p>I use <code>files = ClientUploads.objects.filter(client=current_client).values('file_upload')</code> to get the files, I've tried a few other ways but can't seem to get it to work.  I tried <code>files = ClientUploads.objects.filter(client=current_client)</code> but then I just get a QuerySet object and am not sure how to extract the file names and iterate through.  I really don't understand it.  </p>
  
  <p>Any help would be much appreciated as I am very confused.  I just can't seem to get the file object out of the model.  How can I return a list of the objects and then in the template display the file object from the filefield and also display the created_at field.  I need help understanding how to access the different fields within the model.  If I had a list of file objects within that model I could just iterate through that and the created_at field, but I don't see how to do that.</p>
  
  <p>Any suggestions and examples would help a ton.</p>
  
  <p>Thanks</p>
  
  <p>EDIT:</p>
  
  <p>I also want users to be able to download the files, right now if they click the name it displays the file in the browser as I am serving media.  I most likely will disable this.  But I do need to let the user download the files that are displayed.  How can I accomplish this?  I can't find any information regarding how to let a user download a file.</p>
  
  <p>Thanks</p>