Question

I have an installed app (django_tables2) with its own static files folder but am having issues serving these using {{ STATIC_URL }}. After reading the django docs, if I run

>>> python manage.py findstatic django_tables2/themes/paleblue/css/screen.css

findstatic indeed correctly locates the one matching file within the apps directory of site-packages.

My template for the page in question contains:

{% block extrahead %}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" />
{% endblock %}

And related blocks exist in the parent template.

This page is served at http://127.0.0.1:8000/todo/product_groups/Analytical/. However, when running the development server I get a 404 for the css as its pointing to the wrong location:

GET /todo product_groups/Analytical/django_tables2/themes/paleblue/css/screen.css HTTP/1.1 404 2942

What's going on and why isn't the server following {{ STATIC_URL }} the same way as findstatic? I had this same static_url css working before doing some url redesigns, but can't seem to make it work in the new design. Any help or insight would be much appreciated.

Relevant snippets from settings.py:

MEDIA_URL = '/media/'
STATIC_ROOT = 'C:/Users/riedldar/Documents/Code/Arclin/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    "C:/users/riedldar/Documents/Code/Arclin/Arclin/static",
)
INSTALLED_APPS = ( 
....
'django.contrib.staticfiles',)

# Required for tables2
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
)

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

from urls.py:

from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Arclin.views.home', name='home'),
# url(r'^Arclin/', include('Arclin.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)),

# Todo/Task manager
url(r'^todo/', include('todo.urls')),

# Login/out
url(r'^login/$', 'django.contrib.auth.views.login'),
url(r'^logout/$', 'Arclin.views.logout_page'),

# User Homepage
url(r'^$', 'todo.views.user_home'),

# 
)

and todo\urls.py

from django.conf.urls.defaults import *
#from models import ProductGroup

urlpatterns = patterns('',
    (r'^product_groups/([\w-]+)/$', 'todo.views.items_by_product_group'),
    url(r'^task/(?P<task_id>\d{1,6})$', 'todo.views.view_task',  name='todo_task_detail')
    )
Was it helpful?

Solution

Is todo.views.items_by_product_group rendering your template with RequestContext, or one of the shortcuts or generic views that include it? If not, your template context processors won't be applied to add variables like STATIC_URL to your page context, resulting in the behavior you're describing.

As an aside, Django 1.4 introduces a new {% static '…' %} template tag, which lets you refer to static files without needing STATIC_URL from the context. Using that should also prevent this problem (but you'll probably still want to investigate if or why RequestContext is missing: that's usually a bug).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top