I'm using jquery form plugin http://jquery.malsup.com/form/#getting-started 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.
My HTML code:
<form id="uploadform" method="post" enctype="multipart/form-data" action="background/"> <input type="file" name="photoimg" id="photoimg" /> </form> Javascript code:
https://gist.github.com/2377516
Django Models.py:
class BackgroundModel(models.Model): user = models.OneToOneField(User) background = models.ImageField(upload_to='backgrounds') Django views.py:
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("") Django settings.py:
MEDIA_ROOT = '/home/nirmal/try/files/' MEDIA_URL = 'http://localhost:8000/files/' Django urls.py:
url(r'^cover/$', login_required(direct_to_template), {'template': 'cover.html'}), url(r'^cover/background/$', 'cover.views.backgroundview'), Could anyone help me?
Thanks!