NeuroAgent

Django Images Not Showing: Fix 404 Errors

Troubleshoot Django image 404 errors when uploaded through admin. Complete guide to fix media file display issues with URL patterns, settings, and templates.

Django Image Not Displaying on Website Despite Upload in Admin

I’m building a platform for tradespeople and customers to share reviews and post jobs as a learning project to understand Django, Python, and how they integrate together.

On my jobs page, I need to display active and inactive jobs with images in a Bootstrap card layout. I’ve uploaded images through Django admin, but they’re not showing on the website.

Here’s what I’ve already configured:

  • Pillow is installed
  • MEDIA directory is set up in settings
  • Template includes enctype=“multipart/form-data” and an tag

Despite these configurations, the images aren’t displaying on the site, and I’m getting a 404 error when trying to access them. I’ve attached screenshots showing:

  1. The jobs page without images
  2. Django admin with the uploaded image
  3. The 404 error when trying to access the image

What could be causing this issue, and how can I fix it?

NeuroAgent

Django images uploaded through the admin not displaying on your website and returning 404 errors is a common issue that typically stems from improper URL configuration for media files. The most common cause is that Django’s development server doesn’t serve media files by default, requiring explicit URL patterns to route media requests to the correct directory.


Contents


Understanding the Problem

When you upload images through Django’s admin interface, they’re stored according to your MEDIA_ROOT setting. However, Django doesn’t automatically create URL patterns to serve these files back to the web browser. This is why you see the image in the admin but get a 404 error when trying to access it on your website pages.

As the OverIQ tutorial explains: “The Django development server doesn’t serve media files by default.” This means you need to explicitly configure how media files should be served.


Common Causes of Media File 404 Errors

Based on the research findings, here are the primary causes of Django media file 404 errors:

  1. Missing URL patterns for media files - The most common issue
  2. Incorrect MEDIA_URL and MEDIA_ROOT configuration
  3. URL pattern order preventing media requests from reaching the right handler
  4. File path mismatches between settings and actual file locations
  5. Development vs production server configuration differences

Step-by-Step Solutions

Checking Your Media Configuration

First, verify your Django settings.py has the correct media configuration:

python
# In your settings.py
MEDIA_URL = '/media/'  # URL prefix for media files
MEDIA_ROOT = BASE_DIR / 'media'  # Physical path where media files are stored

Important considerations:

  • MEDIA_URL should match the URL prefix you’re using in your templates
  • MEDIA_ROOT must be an absolute path to your media directory
  • Ensure the media directory actually exists in your project structure

As the Stack Overflow discussion notes, configuration issues here are the root cause of many 404 errors.

Configuring URL Patterns

The critical step is adding URL patterns to serve media files during development. Open your project’s main urls.py file and add the following:

python
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path

urlpatterns = [
    # Your existing URL patterns here
    path('admin/', admin.site.urls),
    # ... other patterns
]

# Add this at the end of urls.py
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Key points:

  • The static() function only works in DEBUG mode
  • Media URL patterns should typically come after your main application URLs
  • The show_indexes=True parameter can be added to display directory listings if needed

As mentioned in the Stack Overflow answer, “some of your patterns in the list prevent the request from reaching the static handlers. So putting the static handlers first solves this.”

Verifying File Paths and Permissions

Check that:

  1. The media directory exists at the location specified in MEDIA_ROOT
  2. Files are actually present in the expected subdirectories
  3. The web server has read permissions for the media files

The mod_wsgi issue emphasizes: “Ensure that the directory /code/files/media exists first to be safe.”

You can verify this by:

  • Checking if the media directory exists: import os; print(os.path.exists(settings.MEDIA_ROOT))
  • Checking if specific files exist: import os; print(os.path.exists(os.path.join(settings.MEDIA_ROOT, 'my_image.png')))

Template Configuration

Ensure your template is correctly referencing media files:

html
<!-- In your template -->
<img src="{{ job.image.url }}" alt="{{ job.title }}" class="card-img-top">

Important: Django automatically generates the full URL using your MEDIA_URL setting when you access the url attribute of a FileField.


Development vs Production Environment Differences

Development Environment

In development (DEBUG=True), you can use the static() helper function as shown above. However, this approach is not recommended for production.

Production Environment

For production, you should configure your web server (nginx, Apache, etc.) to serve media files directly. As the Django Central guide states: “In production, uploaded files should be served from a different domain such as Amazon S3.”

Example nginx configuration:

nginx
location /media/ {
    alias /path/to/your/project/media/;
    expires 1y;
    add_header Cache-Control "public";
}

Troubleshooting Checklist

If you’re still experiencing 404 errors, work through this checklist:

  1. Verify settings.py has proper MEDIA_URL and MEDIA_ROOT
  2. Check that media directory exists and contains uploaded files
  3. Add static() call to urls.py for development
  4. Ensure URL pattern order is correct
  5. Clear browser cache and test in incognito mode
  6. Check browser developer tools for network errors
  7. Verify file permissions
  8. Test accessing media files directly via browser URL

As the testdriven.io guide emphasizes: “Unfortunately, the Django development server doesn’t serve media files by default.”


Sources

  1. Page not found 404 Django media files - Stack Overflow
  2. MEDIA_URL Page 404 Error Django - Stack Overflow
  3. Handling Media Files in Django - OverIQ.com
  4. Working and Configuring Media Files in Django - Medium
  5. Django Media Files - Django The Right Way
  6. How to serve media files on Django production environment - Stack Overflow
  7. Django static media always returning 404 not found - Scott Barnham
  8. Media files in production - Django Forum
  9. Working with Static and Media Files in Django - TestDriven.io
  10. How to handle media files (uploads) in Django - CloudDevs

Conclusion

The primary cause of Django media files not displaying and returning 404 errors is typically missing URL configuration. For your development environment, add the static URL patterns to your urls.py file as described above. Ensure your MEDIA_URL and MEDIA_ROOT settings are correctly configured, verify file paths and permissions, and check that your templates reference the image URL properly.

Remember that this development-only solution using static() should not be used in production. For production deployment, configure your web server to serve media files directly. Following these steps should resolve the 404 errors and get your images displaying properly on your tradespeople platform.