A
A
A1l2e3x42021-12-29 23:23:38
Django
A1l2e3x4, 2021-12-29 23:23:38

How to upload an image to an Imagefield?

models.py

class FinePhoto(models.Model):
    partner = models.ForeignKey(Partner, on_delete=models.CASCADE)
    fine = models.ForeignKey(Fine, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='fine_images', blank=True, null=True)

photo.py

photo = FinePhoto(fine=fine, partner=partner)
photo.image  = добавить фото из статика
photo.save()

How can I add an image to an image from static when creating an instance of a model?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2021-12-29
@A1l2e3x4

Don't do that, static for static.

V
Vladislava, 2021-12-31
@vladis005

You need not a static but a media folder. Set up media. And in ImageField you add upload_to="img/" where it is assumed that img is in media

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question