2

I am using MYSQL for the database. I want to upload my imagefield files in media folder that I have created in my project. I am getting "empty path didn't exist".

settings.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

models.py


class Product(models.Model):
    category = models.ForeignKey(Category, related_name='product', on_delete=models.CASCADE)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='product_creator')
    title = models.CharField(max_length=255)
    author = models.CharField(max_length=255, default='admin')
    description = models.TextField(blank=True)
    image = models.ImageField(upload_to='images/')
    slug = models.SlugField(max_length=255)
    price = models.DecimalField(max_digits=4, decimal_places=2)
    in_stock = models.BooleanField(default=True)
    is_active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name_plural = 'Products'
        ordering = ('-created',)

    def __str__(self):
        return self.title

urls.py

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I have registered the model successfully in admin.py.

  • How it's showing path of image on browser ? – Ankit Tiwari Jul 15 '21 at 06:44
  • Can you show the exact steps you perform when you get the error? Can you share the full error message with the traceback? – GwynBleidD Jul 15 '21 at 13:24
  • In our main page it was showing the path does not exist. But, it's fixed now. I was adding 'MEDIA_ROOT' and 'MEDIA_URL' in settings.py file. After I deleted these path my above code is working. – Unnati Verma Jul 17 '21 at 14:42

2 Answers2

0

your code:

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

test this:

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
0

I fixed this error by deleting the 'MEDIA_URL' and 'MEDIA_ROOT' path in settings.py file that I had previously added. The above code is then working.