Home > Net >  Django: Multiple Photos Uploading not connected to user
Django: Multiple Photos Uploading not connected to user

Time:07-28

i've been trying to upload multiple photos to the gallery of a user and i am being able to do so but they are not being connected to the user who is uploading them. images just gets to the specified path but when i see it in admin panel, photos are there, but no user is connected to them.

#views.py

def images(request):
    owner = request.user.profile
    name = request.POST.get('filename')
    files = request.FILES.getlist('images')

    for file in files:
        i = Images.objects.create(image=file,)
        i.save()
    return render(request, 'profiles/images.html')

#models.py

class Images(models.Model):
    profile = models.ForeignKey(Profile, null=True, blank=True, on_delete=models.CASCADE)
    f_name = models.CharField(max_length=250, null=True)
    image = models.ImageField(upload_to='user/')
    updated = models.DateField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

#forms.py

class ImageForm(forms.ModelForm):
    image = forms.ImageField(label='Image')    

    class Meta:
        model = Images
        fields = ('image',)

CodePudding user response:

You not creating image object with profile. your view should look like:

def images(request):
    owner = request.user.profile
    name = request.POST.get('filename')
    files = request.FILES.getlist('images')

    for file in files:
        i = Images.objects.create(image=file, profile=owner) # you need to add this
        i.save()
    return render(request, 'profiles/images.html')
  • Related