Django Static files Page not found (404)

Viewed 25

I am trying to create a website using Django and in my models.py I am using Django's ImageField(upload_to=''), but these files are not being uploaded to their location and I don't know why. That file is empty. So I am getting this error Page not found (404) at http://127.0.0.1:8000/class/instructor_pics/2_c4iU33j.jpg. The other static files are fine like carousel banner and everything but these files are not being uploaded. What should I change to make this work?

My models.py:

class Course(models.Model):
    title = models.CharField(max_length=100)
    image = models.ImageField(upload_to='class/instructor_pics', null=True)
    instructor = models.CharField(max_length=100)
    instructor_image = models.ImageField(upload_to='class/instructor_pics', null=True)
    enrolled_students = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='enrolled_students', blank=True)

    slug = models.SlugField(max_length=200, unique=True)
    description = models.TextField(max_length=300, null=True)
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created']

    def __str__(self):
        return self.title


    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        img = Image.open(self.image.path)

        if img.height > 285 or img.width > 201:
            output_size = (285, 201)
            img.thumbnail(output_size)
            img.save(self.image.path)

        img2 = Image.open(self.instructor_image.path)

        if img2.height > 40 or img2.width > 40:
            output_size = (40, 40)
            img2.thumbnail(output_size)
            img2.save(self.instructor_image.path)

My settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = []

import sys
sys.modules['fontawesome_free'] = __import__('fontawesome-free')

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'class.apps.ClassConfig',
    'crispy_forms',
    'django_bootstrap_icons',
    'fontawesome_free',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'abrar_class.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
...
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

0 Answers
Related