I got stuck trying to display the Django media files from a Django project served using FastCGI configured in Apache. I thought for sure that the issue must be something with Apache so I tried a ton of different configuration variables. I wanted to share in case anyone else runs into the CSS and image files not showing up for the Django admin site. If the admin login to your Django project does not look similar to the below then the media files are not being loaded properly.
Django Project Admin Login:
When I first logged into my Django project admin site I was not aware that it was supposed to look like the above. Once I figured out something was wrong I tried every Apache configuration addition or modification I could find to get the media files working properly but none of them seemed to work. It ended up being an issue with a setting in the settings.py Django project file which I will explain below. There are four basic items that you need to make sure are configured properly for the media files to serve properly from your Django project using FastCGI’s via Apache.
Items To Verify If A Django Projects Admin Media Files Are Not Displaying:
- Apache Alias: Make sure that an Alias directive has been added to the Apache configuration for your virtual host that tells Apache to send requests for /media to the proper directory. It will look something like the below.
Apache Alias For Django Admin Media Files:
- Alias /media /usr/lib/python2.6/site-packages/django/contrib/admin/media
- Django urls.py: There needs to be a redirect in the urls.py file in the root of your Django project. The configuration line will look similar to the below.
Django Admin Config In urls.py:
- (r'^admin/(.*)', admin.site.root),
- Verify Django Installed Apps: In the settings.py file also located in your Django projects root directory make sure that “django.contrib.admin” is noted under INSTALLED_APPS. It should look something like the below cut from the settings.py file.
Django Admin INSTALLED_APPS In Settings.py:
- 'django.contrib.admin',
- Verify Django Media Variables: Make sure that you have any variables that specify information about the media files set properly. This was my issue and I am not sure if it came in the project like this or I made the change and forgot about it but below I show the one that caused my issue and the change I made to make everything work properly.
The Incorrect Admin Media Prefix:
- ADMIN_MEDIA_PREFIX = '/admin/media/'
The Correct Django Admin Media Variables In Settings.py:
- ADMIN_MEDIA_PREFIX = '/media/'
Hopefully if you are having the issue with the Django admin media files not displaying properly one or all of the tips above will assist. I attempted to troubleshoot way to much and would have resolved the issue much quicker if I had just looked in the configuration files.