1

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!

1 Answer 1

2

Files are in request.FILES not request.POST. You aren't seeing any errors for this because your are never confirming that b actually got a value, though I'm surprised you're not seeing an error for background cannot be null.

Additionally you should try using a ModelForm for this. It will wrap up a lot of the behavior you're having to code in.

Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.