title: File upload form in Django
body: <p>I'm trying to create a simple upload form for my project in <code>Django 1.5</code> an <code>Python 2.7</code>.</p>
  
  <p>This is my <code>File</code> class:</p>
  
  <pre><code>class File(models.Model):
      middleschool = 'MS'
      highschool = 'HS'
      university = 'U'
      blank = '-'
  
      school_choices = ((middleschool, 'Middle School'), (highschool, 'High school'), (university, 'University'), (blank, 'Not defined'),)
  
      name = models.CharField(max_length = 30, primary_key=True, blank=False, null=False)
      description = models.CharField(max_length = 140, blank=False, null=False)
      school = models.CharField(max_length = 30, choices = school_choices, default = blank)
      subject = models.ForeignKey(Subject)
      user = models.ForeignKey(User)
      rating = models.DecimalField(max_digits=2, decimal_places=0, default = 0)
      price = models.DecimalField(max_digits=2, decimal_places=1, default = 0, blank=True, null=True)
      file = models.FileField(upload_to= "/file/")
  </code></pre>
  
  <p>this is the form:</p>
  
  <pre><code>class UploadFileForm(forms.Form):
      middleschool = 'MS'
      highschool = 'HS'
      university = 'U'
      blank = '-'
  
      school_choices = ((middleschool, 'Middle School'), (highschool, 'High school'), (university, 'University'), (blank, 'Not defined'),)
  
      name = forms.CharField(max_length = 30, required = True)
      file = forms.FileField()
      description = forms.CharField(max_length = 140, required = False, label='Breif description of the files content')
      school = forms.ChoiceField(choices = school_choices, required=False, label='What level is the material that are you uploading?', initial = blank)
      subject = forms.ModelChoiceField(queryset=Subject.objects.order_by('?'), required=False, label='What subject this file is about?')
      price = forms.IntegerField(required=False)
  </code></pre>
  
  <p>this is the view:</p>
  
  <pre><code>def upload_file(request):
      if request.method == 'POST':
          form = UploadFileForm(request.POST, request.FILES)
          if form.is_valid():
              new_file = File(file = request.FILE['file'])
              cd = form.cleaned_data
              new_file.name = cd['name']
              new_file.description = cd['description']
              new_file.school = cd['school']
              new_file.subject = cd['subject']
              new_file.price = cd['price']
              new_file.rating = '0.0'
              new_file.user = request.user
              new_file.save()
              form = Search()
              return render(request, 'home.html', {'form': form, 'request': request})
      else:
          form = UploadFileForm()
      return render(request, 'upload.html', {'form': form, 'request': request})
  </code></pre>
  
  <p>and this is the relative <code>HTML</code></p>
  
  <pre><code>{% if request.user.is_authenticated %}
      &lt;form action="" method="post"&gt;
      {% csrf_token %}
          {{ form.as_p }}
          &lt;input type="submit" value="Upload"&gt;
          &lt;input type="reset" value="Reset"&gt;
      &lt;/form&gt;
  {% else %}
      &lt;p&gt;You must be logged to upload a file&lt;/p&gt;
  {% endif %}
  </code></pre>
  
  <p>My app path is: <code>C:/Users/User/Desktop/site_is/app_is/</code> and I want hose files saved in the folder: <code>C:/Users/User/Desktop/site_is/app_is/static/file/</code>. In my <code>Setting.py</code> I set:</p>
  
  <pre><code>MEDIA_ROOT = 'C:/Users/User/Desktop/site_is/app_is/static/file/'
  MEDIA_URL = '/file/'
  STATIC_ROOT = 'C:/Users/User/Desktop/site_is/app_is/static/'
  STATIC_URL = '/static/'
  </code></pre>
  
  <p>The problem is: when i select the file and hit the upload button the <code>FileField</code> empties itself and the form raise an error since that field is required.</p>
  
  <p>I fear I'm doing something wrong with the media/static paths in the <code>Setting.py</code> because the view syntax it's the same as the one in the <a href="https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#handling-uploaded-files-with-a-model" rel="nofollow">Django docmentation</a> but I really don't know how to solve this problem.</p>