Django admin not serving static files?
Asked Answered
H

6

16

Django 1.6

I'm having trouble serving my static files for my Django Admin.

urls.py:

urlpatterns = patterns('',
    url(r'^$', 'collection.views.index', name='home'),
    url(r'^collection/', include('collection.urls')),
    url(r'^admin/',    include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
        url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.STATIC_ROOT,
        }),
)

settings.py

...
MEDIA_ROOT = '/Users/me/projectdir/media/'
MEDIA_URL = 'media/'

STATIC_ROOT = '/Users/me/projectdir/static/'
STATIC_URL = 'static/'
...

template (base.html)

<!DOCTYPE html>
<html lang='en-us'>
<head>
<title>Mysite</title>

{% load static %}
{% block links %}
    <link href="{% static 'css/bootswatch-simplex.css' %}" rel="stylesheet" type="text/css">
    <link href="{% static 'css/custom.css' %}" rel="stylesheet" type="text/css">
    <link rel="shortcut icon" href="{% static "favicon.ico" %}">
{% endblock %}

<script src="{% static "lib/bootstrap-3.1.1-dist/js/bootstrap.js" %}"></script>
<script type="text/javascript">window.__admin_media_prefix__ = "{% filter escapejs %}{% static "admin/" %}{% endfilter %}";</script>
</head>
...

Django is serving my admin OK, just without static files: CSS, JS, etc.

Static files for my public-facing pages work fine.

If I change STATIC_URL to '/static/', then the opposite is true: the admin is fine, but my public pages lose their static files.

Here's the weirdest part. If I "view source" of my admin pages in my browser, it shows the correct URL for static pages, for example:

/static/admin/css/base.css

But if I actually follow the link, it changes it to this:

http://localhost:8000/admin/static/admin/css/base.css

I think it's checking for static files relative to localhost:8000/admin/static/ instead of just localhost:8000/static/. It adds an extra "admin" level to the url, like static is part of the domain. I just can't figure out how to get rid of it.

I have tried collectstatic, but it doesn't help. The static files are in my static directory, they're just not being served. I can type in, say, http://localhost:8000/static/admin/css/base.css and I get the right CSS file (in plaintext). The files are there. I bet something is wrong with my configuration.

I've emptied my caches, restarted my dev server, etc. No beans.

ideas?

Horus answered 19/5, 2014 at 0:56 Comment(5)
Can you show the code in your template where you're trying to access your static files?Adda
You don't need urlpatterns in urls.py. Try removing that and check out by setting STATIC_URL = '/static/'Irreverent
@Alex, I appreciate your hunch. I updated my code above. I also tried removing {% filter escapejs %}{% static "admin/" %}{% endfilter %} to no avail.Horus
@SudipKafle, I'm not sure what you mean? Do you mean to just have a set of url(...)s without any urlpatterns wrappers?Horus
you don't need the extra code after if debug==TRUE.Irreverent
H
8

OK, I figured it out. There was some confusion in my settings files, and I did not have STATICFILES_DIRS correctly set.

In the end, I implemented the version-controlled settings files discussed in Two Scoops of Django 1.6, with this in my settings:

from unipath import Path

BASE_DIR         =  Path(__file__).ancestor(3)
MEDIA_ROOT       =  BASE_DIR.child('media')
STATIC_ROOT      =  BASE_DIR.child('static')

TEMPLATE_DIRS    = (
    BASE_DIR.child('templates'),
)

STATICFILES_DIRS = (
    BASE_DIR.child('myapp').child('static'),
)

STATIC_URL         = '/static/'
MEDIA_URL          = '/media/'

With this, my static files are being served correctly, both in admin and without. My media files, on the other hand, did not work without changing my urls.py in development, according to the accepted answer here. I did not have to do the same for my static files.

Anyways, I hope this helps anyone else banging their head against this particular wall.

Horus answered 20/5, 2014 at 0:37 Comment(0)
F
12

Use django-admin.py collectstatic or go to ~/django/contrib/admin/static and copy the admin folder(which contains the static files) and paste them into your project's static directory.

**EDIT**

A desperate or clumsy solution you can try for: change your STATIC_URL to '/static/', as from question I saw this:

If I change STATIC_URL to '/static/', then the opposite is true: the admin is fine, but my public pages lose their static files.

Then check with inspect element/firebug, see what urls are being served in public pages. Probably a '/' missing or added a '/'. Adjust it, and see if it works.

Francophile answered 19/5, 2014 at 5:33 Comment(8)
the static files are in my static directory, they're just not being served. I can type in, say, http://localhost:8000/static/admin/css/base.css and I get the right CSS file (in plaintext). The files are there. Thanks, though... I updated my question to reflect your idea.Horus
if you are getting the right css by localhost:8000/static/admin/css/base.css then your statics are being served properly. Use firebug and see what urls are you getting for your css files, then adjust them accordingly :)Francophile
thanks, yes that's what I'm trying to get across. I have been spending time with Firebug. I don't know how to 'adjust them accordingly.'Horus
well I answered about that in the answer's edit section. Check what is missing/added. probably a '/'. Then change in your template accordingly, for example href="{% static 'foo/bar.css' %}" to href="{% static '/foo/bar.css' %}"Francophile
Firebug shows the correct relative link. It seems Django is serving static files according to the localhost:8000/admin/ root instead of just localhost:8000/. If so, don't know how to fix. I updated my question. Any idea?Horus
I've been adding random /s everywhere trying to fix this... hasn't worked. Thanks though.Horus
From 'templates/admin/base.html', you will see in line 3: <link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}" />, so admin css being served like localhost:8000/admin/ is the way admin template should be serving css. And random '/' won't work actually :(.Francophile
let us continue this discussion in chatFrancophile
H
8

OK, I figured it out. There was some confusion in my settings files, and I did not have STATICFILES_DIRS correctly set.

In the end, I implemented the version-controlled settings files discussed in Two Scoops of Django 1.6, with this in my settings:

from unipath import Path

BASE_DIR         =  Path(__file__).ancestor(3)
MEDIA_ROOT       =  BASE_DIR.child('media')
STATIC_ROOT      =  BASE_DIR.child('static')

TEMPLATE_DIRS    = (
    BASE_DIR.child('templates'),
)

STATICFILES_DIRS = (
    BASE_DIR.child('myapp').child('static'),
)

STATIC_URL         = '/static/'
MEDIA_URL          = '/media/'

With this, my static files are being served correctly, both in admin and without. My media files, on the other hand, did not work without changing my urls.py in development, according to the accepted answer here. I did not have to do the same for my static files.

Anyways, I hope this helps anyone else banging their head against this particular wall.

Horus answered 20/5, 2014 at 0:37 Comment(0)
S
3

I faced the same issue for two times. The way i solved it was by pasting the static files of admin into static folder mentioned in the code -

cp -r /usr/local/lib/python2.7/site-packages/django/contrib/admin/static/admin /home/ec2-user/mywork-Deployment/mywork/static

This one definitely works and saves a lot of time and troubles. Hope it helps!

Selig answered 23/6, 2019 at 7:0 Comment(0)
C
1

First You need to try:python manage.py collectstatic then u got any errors just follow these steps

step1 **Remove these code from you**r settings.py

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    '/home/uour project/src/project name/static/',
)  //remove these lines

step2

Replace with it replace with these codes

STATIC_ROOT = os.path.join(BASE_DIR, 'static') //add these line

step3 open terminal and type:python manage.py collectstatic

Conchiferous answered 3/7, 2021 at 15:30 Comment(0)
D
1

If it helps anyone, I will share what the issue was with my code. Probably my stupid mistake but may save someone's time:

So, basically. my settings.py variables were something like this:

AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/"
STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/static/"

I inspected the admin page css src tags and found out the URLs were like this: (Notice two forward slashes in the URL)
https://bucket-name.s3.amazonaws.com//admin/css/login.css.

So I changed my variables slightly and everything loaded fine.

AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/static/"

That corrected the faulty URLs and static files loaded perfectly.

Diaphoretic answered 15/2, 2022 at 9:16 Comment(0)
S
0

The way that worked out for me was I referenced the static admin files in my settings.py file. I hope this helps someone :)

'./static/admin/', 
Selig answered 1/10, 2019 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.