title: How to upload a file from webpage and show the name in table in Django
body: <p>I have been doing the research for a long time and cannot figure out. I have my Django app linked to mysql db which was created by my model "CostDetl", and I have a webpage to display my model table. In the table, I have a filefield "Agreement", where I would like the webpage to show browse and upload form in the cell for each table row, and if anything is uploaded and the filefield is not empty, the table cell would just show the file name with a link to open the uploaded file. I have no problem displaying the uploaded file with the link and I can upload the agreement in Admin, but I cannot figure out how to upload from the webpage. Please help! The following are all my code, much appreciated!</p>
  
  <p>Settings:</p>
  
  <pre><code>MEDIA_ROOT = '/Users/username/myapp/media'
  MEDIA_URL = '/media/'
  </code></pre>
  
  <p>In project urls.py, I added:</p>
  
  <pre><code>url(r'^media/', views.UploadAgreementView),
  </code></pre>
  
  <p>In models.py:</p>
  
  <pre><code>def content_file_name(instance, filename):
      return os.path.join(['content', instance.user.username, filename])
  
  
  class CostDetl(models.Model):
      Provider = models.CharField(null=True, blank=True, max_length=200)
      Description = models.CharField(null=True, blank=True, max_length=200)
      Agreement = models.FileField(null=True, blank=True, upload_to=content_file_name)
  
      def __unicode__(self):
          return self.Provider+' | '+self.Description
  
  
      def __str__(self):
          return self.Provider+' | '+self.Description
  </code></pre>
  
  <p>Views:</p>
  
  <pre><code>def CostData(request):
      costrecordset = CostDetl.objects.all()
      context = {'CostDetl1': costrecordset}
      return render(request, 'polls/CostDetl.html', context)
  
  
  def UploadAgreementView(request):
      if request.method == 'POST':
          form = UploadAgreement(request.POST or None, request.FILES or None)
      if form.is_valid():
          newdoc = CostDetl(request.FILES['file'])
          newdoc.save()
  
          return HttpResponseRedirect(reverse('polls:AgreementUpload'))
      else:
          form = UploadAgreement()
  
      documents = CostDetl.objects.all() 
  
      return render(request,
          'polls/CostDetl.html',
          {'CostDetl1': documents, 'AgreementUpload': form})
  </code></pre>
  
  <p>In forms.py:</p>
  
  <pre><code>from django import forms
  from .models import CostDetl
  
  
  class UploadAgreement(forms.Form):
      title = forms.CharField(max_length=50)
      file = forms.FileField()
  </code></pre>
  
  <p>In template:</p>
  
  <pre><code>{% if CostDetl1 %}
      {% load humanize %}
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;th&gt;Provider&lt;/th&gt;
                  &lt;th&gt;Expense Description&lt;/th&gt;
                  &lt;th&gt;Agreement&lt;/th&gt;
              &lt;/tr&gt;
              {% for i in CostDetl1 %}
                  &lt;tr&gt;
                      &lt;td nowrap&gt;{{ i.Provider }}&lt;/td&gt;
                      &lt;td nowrap&gt;{{ i.Description }}&lt;/td&gt;
                      &lt;td nowrap&gt;
                          {% if i.Agreement %}
                              &lt;a href="/media/{{ i.Agreement }}"&gt;
                                  {{ i.Agreement }}
                              &lt;/a&gt;
                          {% else %}
                              &lt;form method="POST" enctype="multipart/form-data"&gt;
                                  {% csrf_token %}
                                  {{ form.as_p }}
                                  &lt;input type="file" name="myfile"/&gt;
                                  &lt;input type="submit" name="submit" value="Upload"/&gt;
                              &lt;/form&gt;
                          {% endif %}
                      &lt;/td&gt;
                  &lt;/tr&gt;
              {% endfor %}
          &lt;/table&gt;
      {% endif %}
  </code></pre>
  
  <p>In urls.py:</p>
  
  <pre><code>url(r'^CostDetl/$', views.CostData, name='CostDetl1'),
  url(r'^Agreement/$', views.UploadAgreementView, name='AgreementUpload'),
  </code></pre>