Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin CSS missing

I've been messing around with the new collectstatic command and have got it working for my normal pages. That is to say, I am able to load my css at this location http://localhost:8000/static/css/main.css. However, the css for my django admin doesn't seem to be showing up.

When I navigate to the admin css location at http://localhost:8000/static/admin/css/base.css, I'm getting a 404 page not found with the following error: /home/nai/GitProjects/cats/django-trunk/django/contrib/admin/media/css/base.css" does not exist. Looking in django-trunk, I never had the /home/nai/GitProjects/cats/django-trunk/django/contrib/admin/media/ folder to begin with.

Is that weird?

In any case, in my static folder, there is an admin folder with the accompanying css, img and js folders which was created when I ran collectstatic and the url of the base.css seems to be pointing to that location.

This is happening on my django development server. Here are some snippets to aid in the bug hunt:

urls

 33 # In order for Dev Server to serve media files for the frontend site.
 34 urlpatterns += staticfiles_urlpatterns()
 35 
 36 try:
 37     if settings.DEBUG: # defined in manage.py when the first arg is "runserver"
 38         urlpatterns += patterns('',
 39         (r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
 40         (r'^media-admin/(?P<path>.*)$', 'django.views.static.serve',{'document_root': os.path.join(settings.MEDIA_ROOT, '..', settings.ADMIN_MEDIA_PREFIX)}),
 41         )
 42 except NameError:
 43     pass

I think it might be something to do with line 40 in my URLS file but changing media-admin to static/admin didnt help.

settings

     58 ADMIN_MEDIA_PREFIX = '/static/admin'
     69 STATIC_ROOT = os.path.join(os.path.abspath(os.path.join(PROJECT_ROOT, '..', MEDIA_DIR, 'static')), '')
     70 
     71 # URL prefix for static files.
     72 # Example: "http://media.lawrence.com/static/"
         73 STATIC_URL = '/static/'
     74 
     75 # Additional locations of static files. Global files are stored in here
     76 STATICFILES_DIRS = (
     77     os.path.join(os.path.abspath(os.path.join(PROJECT_ROOT, '..', 'proj_public', 'static', 'proj')), ''),
     78     )
     79 
like image 564
super9 Avatar asked Aug 30 '11 10:08

super9


4 Answers

Django recommends that you deploy static files with a web server other than wsgi.

  1. In settings.py, set:

STATIC_ROOT = 'static'

  1. Run python manage.py collectstatic, which will copy the Django admin static files to /path/to/project/static/

  2. Configure your static file server. If you use Nginx, you could add this config:

    location /static/ {                              
        alias /path/to/project/static/;  
        expires modified +1w;                        
    }  
    
  3. Reload your web server

You should now have access to the static files.

like image 119
jingping.yi Avatar answered Sep 23 '22 15:09

jingping.yi


In Django 1.4 ADMIN_MEDIA_PREFIX is deprecated. Here are the steps I followed to catch up with these somewhat recent Django changes:

  1. in settings.py, add django.contrib.staticfiles to INSTALLED_APPS

  2. in settings.py define STATIC_URL — the staticfiles app won't run without it. While using runserver they'll get handled magically, but when you deploy, this needs to be a location where those resources can be fetched by a browser.

I think that's all there was to it.

like image 29
Joe Germuska Avatar answered Sep 21 '22 15:09

Joe Germuska


I'm using Django 1.4.3

What did NOT work for me: No matter how much I edited ADMIN_MEDIA_PREFIX in settings.py I noticed no change in the HTML generated for the Django Admin pages. It always says /media/admin/base.css when I view the source.

What DID work for me. Copied the 'admin' folder from /django/contrib/admin/static/ and pasted it into my projects 'media' folder

Now it works great.

like image 27
nu everest Avatar answered Sep 24 '22 15:09

nu everest


It seems dumb, but I actually had this exact issue and the solution was to set DEBUG=False to DEBUG=True on my local dev environment. When debug is set to False, it thinks it's in a production environment which relies on a place to put static files, such as /var/www/html/static, whereas the debug set to True just uses the local directory.

like image 30
Blairg23 Avatar answered Sep 22 '22 15:09

Blairg23