title: File upload in Django
body: <p>I'm trying to get Django to upload a file (keeping it simple, for now), but am having difficulties. Here's all of the relevant code (that I know of).  Perhaps something is wrong with my settings.py? My code is pieces of various answers on Stack Overflow.  I've checked <a href="https://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs" rel="nofollow">here</a> already.</p>
  
  <p><strong>When I select a file (~8.5mb file because I saw Django does something funny if it is under 2.5mb) and press Submit, I am brought to the Upload Success page, but can't seem to find my file anywhere in my project directory.</strong></p>
  
  <p>I am using the development runserver with apache running just for serving images.</p>
  
  <p>settings.py</p>
  
  <pre><code>MEDIA_ROOT = '/Users/adam/Documents/workspace/sitename/media'
  MEDIA_URL = 'http://127.0.0.1:8000/media/'
  STATIC_ROOT = '/Library/WebServer/Documents/static/'
  STATIC_URL = 'http://10.0.1.15/static/'
  ADMIN_MEDIA_PREFIX = '/media/admin/'
  STATICFILES_DIRS = (
  )
  </code></pre>
  
  <p>*the static URL is the address of my local apache webserver.</p>
  
  <p>urls.py</p>
  
  <pre><code>urlpatterns = patterns('',
      ('^$', index),
      ('^uploadfile/$', uploadfile),
      ('^uploadsuccess/$', uploadsuccess),
  )
  
  if settings.DEBUG:
      from django.views.static import serve
      _media_url = settings.MEDIA_URL
      if _media_url.startswith('/'):
          _media_url = _media_url[1:]
          urlpatterns += patterns('',
              (r'^%s(?P&lt;path&gt;.*)$' % _media_url,
              serve,
              {'document_root': settings.MEDIA_ROOT}))
      del(_media_url, serve)
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>def uploadsuccess(request):
    return render_to_response('uploadsuccess.html', {})
  
  class UploadFileForm(forms.Form):
    file = forms.FileField()
  
  def uploadfile(request):
    if request.method == 'POST':
      form = UploadFileForm(request.POST, request.FILES)
      if form.is_valid():
        handle_uploads(request.FILES['file'])
        form.save()
        return HttpResponseRedirect('/')
    else:
      form = UploadFileForm()
  
    return render_to_response('fileupload.html', {'form': form}, context_instance=RequestContext(request))
  
  def handle_uploads(file):
    logging.debug("upload_here")
    if file:
      destination = open('/tmp/'+file.name, 'wb+')
      for chunk in file.chunks():
        destination.write(chunk)
      destination.close()
  </code></pre>
  
  <p>fileupload.html</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  
  &lt;h1&gt;Upload a file&lt;/h1&gt;
  &lt;form enctype="multipart/form-data" method="post" action="/uploadsuccess/"&gt;
    {% csrf_token %}
    &lt;table&gt;
      {% for field in form %}
        {{ field.label_tag }}
        {{ field }}
      {% endfor %} 
    &lt;/table&gt;
    &lt;input type="submit" value="Submit" id="Save"/&gt;
  &lt;/form&gt;
  
  &lt;body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>uploadsuccess.html</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  
  &lt;h1&gt;Upload Successful!&lt;/h1&gt;
  
  &lt;body&gt;
  &lt;/html&gt;
  </code></pre>