Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-cms not routing properly to static url?

Tags:

django-cms

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
like image 208
ineedtosleep Avatar asked Dec 18 '11 06:12

ineedtosleep


2 Answers

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

like image 75
collend Avatar answered Oct 16 '22 17:10

collend


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.

like image 20
ineedtosleep Avatar answered Oct 16 '22 17:10

ineedtosleep