Apache not serving django admin static files
Solution 1
I think you should change:
Alias /media/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"
to:
Alias /static/admin/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"
Because you have:
ADMIN_MEDIA_PREFIX = '/static/admin/'
Solution 2
That's because you haven't setup your STATIC files...
Add to settings:
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/static/'
Then run "python manage.py collectstatic"
That will put all the files under STATIC_ROOT which STATIC_URL will serve... You shouldn't point Apache at your Python lib files!!
If you want your own app-specific static files as well, setup "STATICFILES_DIRS".
Solution 3
I got solution, I looked at the access_log files inside /var/log/httpd/
127.0.0.1 - - [28/Dec/2013:14:49:20 -0500] "GET /static/admin/css/login.css HTTP/1.1" 200 836 "http://127.0.0.1/admin/" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 CentOS/3.6.24-3.el6.centos Firefox/3.6.24"
so I added following tags in /etc/httpd/conf/httpd.conf file,
Alias /static /usr/lib/python2.6/site-packages/django/contrib/admin/static
inside <VirtualHost 127.0.0.1:80>
tag
then I restarted service using
service httpd restart
and it Works!!!
Related videos on Youtube
hobbes3
Updated on July 09, 2022Comments
-
hobbes3 almost 2 years
Let me thanks you guys at the Stack Overflow community for helping me with various Django and Apache (with mod_wsgi) errors. I've asked about 5 related questions so far and now I'm getting closer and closer to getting my content out on a production site!
So I know there are many similar questions about this and I have read a bunch of questions about serving static media files on Django.
I read about
STATIC_URL
,STATIC_ROOT
, the (soon to be obsolete)ADMIN_MEDIA_PREFIX
, and setting aAlias /media/ ...
in the Apache configuration. I tried to test out each solution one by one, but I couldn't get anything working.Here is what my admin site looks like right now
I'm also having a weird case where any subdomain works on my server. For example I was trying to set up my server so that http://www.satoshi.example.com/ would allow my normal (non-Django) content, while http://django.satoshi.example.com/ would allow my Django content to be served. But currently any subdomain, whether satoshi.example.com or blahblahasdas.satoshi.example.com is serving my Django files (I know because I can go to the
/admin
page on both site, although they will be in different sessions).Anyway here are my files on the server which is running
CentOS
(not sure which version),Apache 2.2.15
,Python 2.6.6
,django 1.3.1
, andmod_wsgi 3.2
.I will post what I think is the most relevant files and configuration below:
Apache throws these errors everytime I restart
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored [Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored [Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored [Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored [Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored [Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored [Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored [Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored [Wed Feb 29 01:45:36 2012] [notice] SIGHUP received. Attempting to restart [Wed Feb 29 00:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored [Wed Feb 29 01:45:36 2012] [notice] Digest: generating secret for digest authentication ... [Wed Feb 29 01:45:36 2012] [notice] Digest: done [Wed Feb 29 01:45:36 2012] [warn] mod_wsgi: Compiled for Python/2.6.2. [Wed Feb 29 01:45:36 2012] [warn] mod_wsgi: Runtime using Python/2.6.6. [Wed Feb 29 01:45:36 2012] [notice] Apache/2.2.15 (Unix) mod_auth_pgsql/2.0.3 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.0-fips mod_wsgi/3.2 Python/2.6.6 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations
Here is
/var/www/html/mysite/apache/apache_django_wsgi.conf
which gets loaded into myhttpd.conf
with the optionNameVirtualHost *:80
<VirtualHost *:80> ServerName django.satoshi.example.com ErrorLog "/var/log/httpd/django_error_log" WSGIDaemonProcess django WSGIProcessGroup django Alias /media/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media" <Directory "/usr/lib/python2.6/site-packages/django/contrib/admin/media"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory> <Directory "/var/www/html/mysite"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory> WSGIScriptAlias / "/var/www/html/mysite/apache/django.wsgi" <Directory "/var/www/html/mysite/apache"> Order deny,allow Allow from all </Directory> </VirtualHost>
Here is
/var/www/html/mysite/apache/django.wsgi
import os import sys paths = [ '/var/www/html/mysite', '/var/www/html', '/usr/lib/python2.6/site-packages/', ] for path in paths: if path not in sys.path: sys.path.append(path) os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
And finally here is part of
/var/www/html/mysite/settings.py
# Absolute filesystem path to the directory that will hold user-uploaded files. # Example: "/home/media/media.lawrence.com/media/" MEDIA_ROOT = '' # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash. # Examples: "http://media.lawrence.com/media/", "http://example.com/media/" MEDIA_URL = '' # Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # Example: "/home/media/media.lawrence.com/static/" PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__)) STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static') # URL prefix for static files. # Example: "http://media.lawrence.com/static/" STATIC_URL = '/static/' # URL prefix for admin static files -- CSS, JavaScript and images. # Make sure to use a trailing slash. # Examples: "http://foo.com/static/admin/", "/static/admin/". ADMIN_MEDIA_PREFIX = '/static/admin/' # Additional locations of static files STATICFILES_DIRS = ( # Put strings here, like "/home/html/static" or "C:/www/django/static". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. ) # List of finder classes that know how to find static files in # various locations. STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', # 'django.contrib.staticfiles.finders.DefaultStorageFinder', )
Let me know if you guys need any other files. Thanks in advance!
-
Chris Pratt about 12 yearsExcellent question. You've done your homework and provided plenty of information to work with. Keep it up.
-
edu222 over 11 yearsGreat question, I was also lost and adding ADMIN_MEDIA_PREFIX to my setting file did the trick.
-
-
Chris Pratt about 12 yearsThat only applies to Django 1.4, which is not an official release yet (though it is dropping very soon). In Django 1.3, you need
ADMIN_MEDIA_PREFIX
. It won't work without it. -
jpic about 12 yearsThanks for the heads up, I think he just needs to change his Alias to match is ADMIN_MEDIA_PREFIX after all.
-
hobbes3 about 12 yearsGreat! That worked perfect. Wow that was an easy solution that I overlooked... Later I'll have to do the standard thing and have something like lighthttpd handle static files. But that will be another question on another day :P. Thanks @jpic. You have helped me out before too :-).
-
conorgriffin over 11 yearsShouldn't 'Alias /static/admin/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"' have a trailing '/'?
-
aiham about 11 yearsOn Django 1.5 with Ubuntu 12.10 I had to use: "Alias /static/admin/ "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/"
-
Alessandro Da Rugna almost 11 yearsPerfect answer!! Also thanks @aiham for this last detail on ubuntu
-
Miquel over 10 yearsThanks! Just a detail: If you also have the Alias /static/ for the general static files, be sure to include the Alias /static/admin/ before the general one.
-
doniyor over 9 years@aiham you saved my ass bro
-
bdf over 8 yearsThis should be the accepted answer - collectstatic moves necessary files to your static dir and serves them from there.