Uploading images from two formsets - Django - django

I have a page where you can add a product, add a thumbnail, and add images for the product.
I am using to formsets, seen below. The problem is that images uploaded to the image formset are uploaded correctly, but images uploaded to the thumbnail formset are not uploading at all... what might I be doing wrong?
def AddProduct(request):
ImageFormSet = modelformset_factory(ProductImage,
form=ImageForm, extra=4)
ThumbnailFormSet = modelformset_factory(ProductvThumbnail,
form=ThumbnailForm)
if request.method == 'POST':
product_form = AddEditProductForm(request.POST)
image_formset = ImageFormSet(request.POST, request.FILES, prefix='images',
queryset=roductImage.objects.none())
thumbnail_formset = ThumbnailFormSet(request.POST, request.FILES, prefix='thumbnail',
queryset=ProductThumbnail.objects.none())
if product_form.is_valid() and image_formset.is_valid() and thumbnail_formset.is_valid():
product = product_form.save()
thumbnails = thumbnail_formset.save(commit=False)
for thumbnail in thumbnails:
thumbnail.product = product
thumbnail.save()
images = image_formset.save(commit=False)
for image in images:
image.product = product
image.save()
return HttpResponseRedirect('/product/')
else:
print (product_form.errors, image_formset.errors, thumbnail_formset.errors)
else:
product_form = AddEditProductForm()
image_formset = ImageFormSet(queryset=ProductImage.objects.none(), prefix='images')
thumbnail_formset = ThumbnailFormSet(queryset=ProductThumbnail.objects.none(), prefix='thumbnail')
return render(request, 'product/add.html',
{'product_form': product_form, 'image_formset': image_formset,
'thumbnail_formset': thumbnail_formset},
context_instance=RequestContext(request))

I have found a solution - my formsets seem to be fine, the issue was in my models.py. The ImageField in the ProductThumbnail model was called 'image = models.ImageField...' but it should have been called thumbnail - that is why no thumbnails were being uploaded - my for loop was not finding anything.

Related

For loop on form not functing properly

I have a feature that allows users to upload multiple images to their blog but it is not working correctly. When a user uploads multiple images only one of them is uploaded to the postgres db.
view
def DetailPostView(request, pk):
model = Post
post = Post.objects.get(pk=pk)
if request.method == 'POST':
test = PostImagesForm(request.POST, request.FILES)
files = request.FILES.getlist('images')
if test.is_valid():
for f in files:
instance = test.save(commit=False)
instance.post = Post.objects.get(pk=pk)
instance.save()
else:
print(instance.errors)
postgallery = PostImages.objects.filter(post_id=post)
context = {
'post':post, 'PostImagesForm':PostImagesForm, 'postgallery':postgallery
}
return render(request, 'blog/post_detail.html', context)
form
class PostImagesForm(ModelForm):
class Meta:
model = PostImages
fields = ('images',)
widgets = {
'images': forms.ClearableFileInput(attrs={'multiple': True}),
}
you can see i am getting the list of files via the files = request.FILES.getlist('images') then running a for loop on the contents.
If I break the code in the stack trace I can see that the two files are in the list so i am very confused on why it is not properly iterating though the list and uploading each file to the db.
Update
Took a look into the docs and found a section on multi image upload and the docs are doing it the same way I am. Still very confused.
I believe the issue was the because I was not passing the current image into the model so it only uploaded the first image.
Solution
if request.method == 'POST':
for f in request.FILES.getlist('images'):
test = PostImagesForm(request.POST, request.FILES)
if test.is_valid():
instance = test.save(commit=False)
instance.post = Post.objects.get(pk=pk)
instance.images = f
instance.save()

How to get image data from Django form which returns False for is_valid()

How to save image in Django form that is not validated. I could able to save data of Charfield using
title = form_name["title"].data
Other data that are passed are getting saved but image is not getting saved. How to fix it.
form = forms.FormName()
if request.method == "POST":
form_name = FormName(data = request.POST)
title = form_name["title"].data
thumbnail = form_name["thumbnail"].data
content = form_name["content"].data
tags = form_name["tags"].data
instance = htmlpage(title=title, thumbnail=thumbnail, content= content, tags=tags, by=request.user)
instance.save()

Dropzone with django image upload

Im trying to upload images using Dropzone.js .
There doesnt seem to be any current tutorials for Dropzone although i used this link: https://amatellanes.wordpress.com/2013/11/05/dropzonejs-django-how-to-build-a-file-upload-form/
Essentially what i want to do is , The user uploads multiple images . a hotel_id is attached to the images and stored in the hotelphotos table as a url which is unique each time .
MY code
Models.py
class Photo(models.Model):
hotel = models.ForeignKey(Hotels,on_delete=models.CASCADE)
path = models.FileField(upload_to='files/%Y/%M/%d)
forms.py
class PhotosForm(forms.ModelForm):
class Meta:
model = Photo
fields = ['path']
views.py
def uploadPhoto(request,hotelid):
if request.method == 'POST':
form = PhotosForm(request.POST,request.FILES)
if form.is_valid():
new_file = Photo(path = request.FILES['file'] , hotel_id = hotelid)
new_file.save()
else:
form = PhotosForm()
hotelid = hotelid
data = {'form': form, 'hotelid':hotelid}
return render(request, template , data)
The form
<form class="dropzone" action="{% url 'ManageHotels:uploadPhoto' hotelid %} method = "POST">
</form>
Files uploaded dont get created and the url also doesnt add to the database.
Hope someone can help!

Formset populated by wrong data - Django 1.8

Basic problem: I have a list of products, and when I go to edit a single product, the image upload formset is populated with all images in the folder, but it should only be populated by related images (see left hand side of picture below).
When I try to make any changes - to the product or to the images - it is unsuccessful, and the input boxes which were populated by unrelated images now display 'this field is required' (see right hand side of picture below)
Obviously, I need to successfully edit the product object, and I want this page to be populated only by images related to that product.
The View
def EditProduct(request, pk):
instance = get_object_or_404(Product, id=pk)
ImageFormSet = modelformset_factory(ProductImage,
form=ImageForm, extra=4)
if request.method == 'POST':
product_form = AddEditProductForm(request.POST, request.FILES, instance=instance)
formset = ImageFormSet(request.POST, request.FILES,
queryset=ProductImage.objects.none())
if product_form.is_valid() and formset.is_valid():
product = product_form.save()
images = formset.save(commit=False)
for image in images:
image.product = product
image.save()
return HttpResponseRedirect('/product/')
else:
print (product_form.errors, formset.errors)
else:
product_form = AddEditProductForm(instance=instance)
formset = ImageFormSet(queryset=ProductImage.objects.all()) # possible wrong queryset
return render(request, 'product/edit.html',
{'product_form': product_form, 'formset': formset},
context_instance=RequestContext(request))
So this code is somehow returning ALL of my uploaded images - including unrelated images - to the editing form for a single product - how do I make it so only those images which are related to the instance are displayed in the form?
On a related note - how do you access an individual image in the formset - for example if i wanted to remove one, what kind of modifications am I looking at?
This depends on your model.
formset = ImageFormSet(queryset=ProductImage.objects.all()) # possible wrong queryset
This is indeed the wrong queryset. You should do something like:
queryset=ProductImage.objects.filter(product_id = pk)
if you gave the product_image field a related_name than you could do
queryset=instance.related_product_images
https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ForeignKey.related_name

Django: Referencing the pk in a multi form view

I have a view with two forms. I am trying to reference the uploaded image's pk in the same form. How do i do that?
form1 = ImageForm(request.FILES, request.POST)
form2= SongForm(request.POST or None)
if form2.is_valid() and form1.is_valid():
image = form1.save(commit=False)
image.save()
save_file(request.FILES['image'])
song = form2.save(commit=False)
song.image = the uploaded image's pk???
song.save()
Thanks,
Once you call image.save() image should get a pk, and therefore, you should be able to reference it with image.pk.