Home > other >  I would like to save my crop image to my django database called model filed file
I would like to save my crop image to my django database called model filed file

Time:01-08

I have the following models.py

class Imageo(models.Model):
    file = models.ImageField(upload_to='cropped_images')
    uploaded = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.pk)

I would like to savemy file into my Imageo models but it is saving into my database some other folder, please look at the views.py file.

def con(request):
    if request.method == 'POST':
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid():
            image = Image.open(form.cleaned_data['file'])
            image = image.resize((200, 200), PIL.Image.ANTIALIAS)
            image = image.convert('RGB')
            dt = image.save('resize.jpg')
            # i would like to save resize.jpg into my models name called file
            return HttpResponse('done')
    else:
        form = ImageForm()

    img = Imageo.objects.latest('id')
    return render(request, 'NEC/imgCon.html', {'form': form, 'img': img})

CodePudding user response:

import tempfile

if request.method == 'POST':
    form = ImageForm(request.POST, request.FILES)
    if form.is_valid():
        # Open the image file
        image = Image.open(form.cleaned_data['file'])
        # Resize the image to a size of 200x200
        image = image.resize((200, 200), PIL.Image.ANTIALIAS)
        # Convert the image to the RGB format
        image = image.convert('RGB')
        # Create a temporary file
        with tempfile.NamedTemporaryFile(suffix='.jpg') as temp:
            # Save the image to the temporary file
            image.save(temp)
            # Seek to the beginning of the file
            temp.seek(0)
            # Create a new Imageo instance
            imageo = Imageo()
            # Assign the temporary file to the file field of the instance
            imageo.file.save('resize.jpg', temp)
            # Save the instance to the database
            imageo.save()
        return HttpResponse('done')
else:
    form = ImageForm()

img = Imageo.objects.latest('id')
return render(request, 'NEC/imgCon.html', {'form': form, 'img': img})

CodePudding user response:

To save the file to your Imageo model, you can create a new instance of Imageo and assign the file to the file field. You can then save this instance to the database using the save() method.

image_instance = Imageo()
image_instance.file = request.FILES['file']
image_instance.save()

This will save the file to the file field of the Imageo model and then save the instance to the database.

You can also create and save the Imageo instance in a single line using the create() method:

image_instance = Imageo.objects.create(file=request.FILES['file'])

Note that in both cases, you need to have a valid request.FILES['file'] in order for this to work. You can access the file from the request.FILES dictionary, which holds all uploaded files.

  • Related