django-cms not routing properly to static url?
Asked Answered
M

4

6

I'm still relatively new to Django and have just started poking around at Django-CMS, however, I can't get past the basic introduction from the official docs.

My problem may be related to sekizai, but it feels like it's a basic url issue in urls.py.

Basically, I have followed the tutorial almost exactly. The only difference is that I have my cms app under blog/, full path ~/workspace/djangocms/blog/. I have set the STATIC_URL and STATIC_ROOT properly under settings.py and the same goes with my MEDIA_URL and MEDIA_ROOT.

I bring up the media path and directory because if I set my base template to link to css at {{ MEDIA_URL }}css/somecss.min.css it works fine. However, doing the same with STATIC_URL: {{ STATIC_URL }}css/somecss.min.css doesn't work and produces 404s.

Also, from what I can tell, the default /static/ routes seem to work fine for other directories. The code produced by {% cms_toolbar %} generates fine and css from places like /static/cms/css/plugins/cms.toolbar.css are being served properly.

Contents of urls.py

from django.conf.urls.defaults import *                                         
from django.contrib import admin
from django.conf import settings

admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'blog.views.home', name='home'),
    # url(r'^blog/', include('blog.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
    (r'^admin/', include(admin.site.urls)),
    url(r'^', include('cms.urls')),
)
if settings.DEBUG:
    urlpatterns = patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT,
        'show_indexes': True}),
        url(r'',
        include('django.contrib.staticfiles.urls')),
    ) + urlpatterns
Marquita answered 18/12, 2011 at 6:50 Comment(0)
M
6

Got my answer from #django on freenode. Answering it myself here in case anyone needs it:

Mainly for django-cms, STATIC_ROOT is mainly used for assets from django-cms itself during production. If django is being run using the python manage.py runserver method, STATIC_ROOT is irrelevant.

From the Django docs: Use collectstatic to collect all the static assets used (which for django-cms will probably be under a directory like /usr/local/lib/python##/dist-packages). This shows which files STATIC_ROOT will use during production.

If you want to serve other directories with STATIC_URL, you will have to add them to the STATICFILES_DIRS section of settings.py. (Make sure to read the comments -- use the absolute paths and not relative paths). This is actually relatively obvious when making a pure Django app, however, because I wanted to strictly follow the Django-cms tutorial this was not as obvious as it should've been.

Marquita answered 21/12, 2011 at 18:53 Comment(0)
S
8

I went round and round with this problem after following the same tutorial.

My site would load the standard welcome page and I could create CMS pages. However, when I tried to load static files in my templates I kept getting 404 errors.

The answer given by the OP helped me a lot.

Just to be clear, this worked for me:

  1. Rename the static directory in the project to something like local_static

  2. Add the following to settings.py

PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))

STATICFILES_DIRS = ( os.path.join(PROJECT_PATH, "local_static/"), )

This worked for me. I can now stop pulling my hair out

Sorel answered 6/12, 2012 at 17:54 Comment(0)
M
6

Got my answer from #django on freenode. Answering it myself here in case anyone needs it:

Mainly for django-cms, STATIC_ROOT is mainly used for assets from django-cms itself during production. If django is being run using the python manage.py runserver method, STATIC_ROOT is irrelevant.

From the Django docs: Use collectstatic to collect all the static assets used (which for django-cms will probably be under a directory like /usr/local/lib/python##/dist-packages). This shows which files STATIC_ROOT will use during production.

If you want to serve other directories with STATIC_URL, you will have to add them to the STATICFILES_DIRS section of settings.py. (Make sure to read the comments -- use the absolute paths and not relative paths). This is actually relatively obvious when making a pure Django app, however, because I wanted to strictly follow the Django-cms tutorial this was not as obvious as it should've been.

Marquita answered 21/12, 2011 at 18:53 Comment(0)
B
1

The solution is so simple I missed it too.

from settings.py;

STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'mysitename', 'static'),
)

If you look inside 'mysitename' there is another folder named 'static'. If you place your files there, everything will work as you may expect it to. The 'static' folder in the same level as manage.py is a red herring.

Buzz answered 12/9, 2016 at 23:48 Comment(0)
E
0

I posted the same answer here, but I was encountering the same issues. While I was able to serve static files for everything else, for some reason my django-cms files kept returning a 404. I checked settings.py and permissions but I did not have much luck.

On Ubuntu 14.04 running Nginx and Gunicion I as able to solve the problem by editing:

sudo nano /etc/nginx/sites-enabled/django

And I added the following code block

location /static/cms {
    alias /usr/local/lib/python2.7/dist-packages/cms/static/cms/;
}

Then I restarted Nginx and Gunicorn

sudo service nginx restart && sudo service gunicorn restart

and I was able to see all the missing static files

Eliezer answered 1/5, 2015 at 15:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.