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/")
settings.pyfileMEDIA_ROOTandMEDIA_URLconfig?