Answer a question

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?

Answers

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.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐