1

I am trying to let my users upload multiple pictures to their blog posts. I have created a model for the blog post and a separate model for the images and used a foreign key to relate them. I was planning on using dropzone.js so that the user can drag and drop the pictures. I have looked into using formsets but can't get my head around them. Can anyone explain to a django beginner how to go about doing this? Or better yet give an example? Thanks!

2
  • Do you need to attach the images before saving the blog post instance? Dec 5, 2015 at 15:58
  • @LorenzoPeña yes ideally before saving the blog post instance. Any advice / help would be much appreciated
    – ollysmall
    Dec 5, 2015 at 16:26

1 Answer 1

0

I have done something similar using jquery-fileupload. Never used dropzone.js so maybe the explanation is not directly equivalent, but this is roughly what you do.

If you don't want to use formsets, upload the images via AJAX and add hidden fields to the form with the primary keys of the uploaded images, so you can attach them when saving the blogpost in Django. You will have to deal with orphaned images (those that were uploaded but the blogpost was never saved.)

If you want to use formsets, Django expects a format in the posted form data, you just need to make sure you create in your html (with dropzone js templating) the apropriate format Django is expecting, and also incrementing/decrementing the counter of the formset's management form.

Neither way is trivial, you need to pick a path and bang your head a few times before having it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.