Friday, March 25, 2011

Django 1.3 - Question about static files

Hello everyone!

I'm using Python 2.7.x with Django 1.3 and I've some questions about
the new way to set the static files for the dev server.

My Settings:
-------------------------------------------------------------------------------------------------------------
abspath = lambda *p: os.path.abspath(os.path.join(*p))
PROJECT_ROOT = abspath(os.path.dirname(__file__))
(...)
MEDIA_ROOT = abspath(PROJECT_ROOT, 'media')
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/admin/'

STATIC_ROOT = abspath(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
abspath(STATIC_ROOT, 'css'),
abspath(STATIC_ROOT, 'javascript'),
abspath(STATIC_ROOT, 'images'),
)
(...)
-------------------------------------------------------------------------------------------------------------
My URLConf
-------------------------------------------------------------------------------------------------------------
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.base import TemplateView
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name='base.html')),
url(r'^accounts/', include('userena.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += staticfiles_urlpatterns()

#if settings.DEBUG:
# urlpatterns += patterns('',
# (r'^static/(?P<path>.*)$',
# 'django.views.static.serve',
# {'document_root': settings.STATIC_ROOT, 'show_indexes': True, }),
#)
-------------------------------------------------------------------------------------------------------------
My base template
-------------------------------------------------------------------------------------------------------------
{% load static %}
(...)
<link type="text/css" href="{% get_static_prefix %}css/default.css" />
(...)
-------------------------------------------------------------------------------------------------------------

*** I create a test app with a /static/ directory and try the
static_urlpatterns as described in the docs:

http://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/

This first option doesn't work! (acordind the doc, the function checks
automatically if the DEBUG is True.)

*** So, I tried the "old way", as you can see in the commented lines
on my URLConf.

This second test doesn't work too...

*** I made a last try, I send my static files to /media/ and continues
with the "old way", only changing static to media in the URLConf and
in the base template:

My URLConf
-------------------------------------------------------------------------------------------------------------
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.base import TemplateView
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()
(...)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True, }),
)
-------------------------------------------------------------------------------------------------------------
My base template
-------------------------------------------------------------------------------------------------------------
{% load static %}
(...)
<link type="text/css" href="{% get_media_prefix %}css/default.css" />
(...)
-------------------------------------------------------------------------------------------------------------

And now... Works!

I'm really don't understand what happened. I want to use the
staticfiles app and need your help to understand how to correctly
configure a project on the dev server.

Thanks in advance!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment