1

I have been struggling with this for the past days i have done everything right and i dont know what am missing exactly.

when i try to upload the image from the admin page it uploads succesfully creates the media folder if not exist already.

but when i try to get the image from url the image is saved in the url field of my model but never get create to the image field of my model whis is image = image = models.ImageField

below is my code

models

class Image(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, related_name="user_images", on_delete=models.CASCADE
    )
    users_like = models.ManyToManyField(
        settings.AUTH_USER_MODEL, related_name="images_liked", blank=True
    )
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, blank=True)
    url = models.URLField()
    image = models.ImageField(upload_to="images/%Y/%m/%d/")
    description = models.TextField(blank=True)
    created = models.DateField(
        auto_now_add=True, db_index=True
    )

forms save method

def save(self, force_insert=False, force_update=False, commit=True):
            image = super().save(commit=False)
            image_url = self.cleaned_data["url"]
            name = slugify(image.title)
            extension = image_url.rsplit(".", 1)[1].lower()
            image_name = f"{name}.{extension}"
            # download image from the given URL
            response = request.urlopen(image_url)
            image.image.save(image_name, ContentFile(response.read()), save=False)
            if commit:
                image.save()
            return image
STATIC_URL = "/static/"
STATICFILES_DIRS = [BASE_DIR / "static/"]  # routes to search for static files
STATIC_ROOT = os.path.join(
    BASE_DIR, "staticfiles/"
)  # django collects all static files here


# MEDIA
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
2
  • Could you add your settings.py file MEDIA_ROOT and MEDIA_URL config? Commented Jun 3, 2021 at 19:40
  • i think the media root is okay because when i upload through the admin page everything works fine ... but i want to save the downloaded image to the model field (image) Commented Jun 3, 2021 at 19:55

1 Answer 1

1

In your views.py, while trying to extract data from the form, instead of

form_data=MyForm(request.POST)

replace it by

form_data = Myform(request.POST,request.FILES)

Also, try to validate your form input by over-riding the initial validate function and make sure, you receive an image with proper format to make your life easier

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks and yes am already validating the form input also am using the form_data=MyForm(data=request.POST) which gets all the data

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.