title: Django jquery ajax image upload
body: <p>I'm using jquery form plugin <a href="http://jquery.malsup.com/form/#getting-started" rel="nofollow">http://jquery.malsup.com/form/#getting-started</a> to upload images to django server side. There is no error but the image is not stored in the MEDIA_ROOT path. I've tried various techniques(tried plugins) for the past 2 days but nothing is working. Below I've given everything to make it easy for you.</p>
  
  <p>My HTML code:</p>
  
  <pre><code>&lt;form id="uploadform" method="post" enctype="multipart/form-data" action="background/"&gt;
  &lt;input type="file" name="photoimg" id="photoimg" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Javascript code:</p>
  
  <p><a href="https://gist.github.com/2377516" rel="nofollow">https://gist.github.com/2377516</a></p>
  
  <p>Django Models.py:</p>
  
  <pre><code>class BackgroundModel(models.Model):
      user = models.OneToOneField(User)
      background = models.ImageField(upload_to='backgrounds')
  </code></pre>
  
  <p>Django views.py:</p>
  
  <pre><code>    def backgroundview(request):
          if request.is_ajax():
              b = request.POST.get('photoimg')        
          try:
              g = BackgroundModel.objects.get(user=request.user)
          except BackgroundModel.DoesNotExist:
              bm = BackgroundModel(background=b)
              bm.user = request.user
              bm.save()
          else:
              g.background = b
              g.save()
          return HttpResponse("")
  </code></pre>
  
  <p>Django settings.py:</p>
  
  <pre><code>MEDIA_ROOT = '/home/nirmal/try/files/'
  MEDIA_URL = 'http://localhost:8000/files/'
  </code></pre>
  
  <p>Django urls.py:</p>
  
  <pre><code>url(r'^cover/$', login_required(direct_to_template), {'template': 'cover.html'}),
  url(r'^cover/background/$', 'cover.views.backgroundview'), 
  </code></pre>
  
  <p>Could anyone help me?</p>
  
  <p>Thanks!</p>