Django admin is not for static files?

Django 1.6

I am having trouble maintaining 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 for my admin, just without static files: CSS, JS, etc.

Static files for my open pages work fine.

If I change STATIC_URLto '/static/', the opposite is true: the administrator is fine, but my public pages are losing their static files.

Here's the weirdest part. If I "browse the 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 really follow the link, she changes it to this:

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

, localhost:8000/admin/static/, localhost:8000/static/. "admin" URL-, static . , .

collectstatic, . , . , , http://localhost:8000/static/admin/css/base.css CSS ( ). . , - .

, dev- .. No beans.

?

+6
4

, . , . , STATICFILES_DIRS.

, , Django 1.6, 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/'

, admin, . , urls.py , . .

, , - .

+5

django-admin.py collectstatic ~/django/contrib/admin/static ( ) .

** EDIT **

, : STATIC_URL '/static/', :

STATIC_URL '/static/', : admin , .

inspect element/firebug, , URL- . , '/' '/'. , .

+5

. , admin , -

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

. , !

+1

For me, it worked like this: I referenced the static admin files in my settings.py file. Hope this helps someone :)

'./static/admin/', 
0
source

Source: https://habr.com/ru/post/1541056/


All Articles