Joseph Roberts
Joseph Roberts

Reputation: 569

Django: static files

When i try to serve up images from within my blog the template is looking in

[26/Mar/2012 10:33:42] "GET /blog/images/coke.jpg HTTP/1.1" 200 6153

However when i load up images from a flat page the template looks in

[26/Mar/2012 10:33:42] "GET /blog/images/coke.jpg HTTP/1.1" 200 6153

I think this is because i need to add something to my urls.py file with my blog app so that it knows what my static URL is rather than looking in the blog/images folder.

Im using the tag:

src="{{ STATIC_URL }}images/header.png"

My settings.py has:

STATIC_ROOT = '/export/mailgrp4_a/sc10jbr/WWWdev/dbe/djangostatic/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/export/mailgrp4_a/sc10jbr/WWWdev/dbe/static/',

My blog url.py has:

urlpatterns = patterns('dbe.blog.views',
(r"^(\d+)/$", "post"),
(r"^add_comment/(\d+)/$", "add_comment"),
(r"^delete_comment/(\d+)/$", "delete_comment"),
(r"^delete_comment/(\d+)/(\d+)/$", "delete_comment"),
(r"^month/(\d+)/(\d+)/$", "month"),
(r"", "main"),
)

My main url.py has:

urlpatterns = patterns('',
url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)

What do i need to add?

My updated blog url.py has:

from django.conf.urls.defaults import *
from dbe.blog.models import *
from django.contrib.staticfiles.urls import staticfiles_urlpatterns


urlpatterns = patterns('dbe.blog.views',
(r"^(\d+)/$", "post"),
(r"^add_comment/(\d+)/$", "add_comment"),
(r"^delete_comment/(\d+)/$", "delete_comment"),
(r"^delete_comment/(\d+)/(\d+)/$", "delete_comment"),
(r"^month/(\d+)/(\d+)/$", "month"),
(r"", "main"),
)

urlpatterns += staticfiles_urlpatterns()

My updated main url.py has:

from django.conf.urls.defaults import *
from dbe.blog.models import *
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
url(r'^blog/', include('blog.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)),

)

urlpatterns += staticfiles_urlpatterns()

Can anyone else help with this, im really stuck even after following the help below. Is there anything i can use to help debug and diagnose the problem?

thanks

Upvotes: 0

Views: 2347

Answers (3)

Jezulex
Jezulex

Reputation: 36

While you are developing you can add this to urls.py. Once you finish you must configure the web server to serve static files.

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,
        }),
   )

Upvotes: 2

Chris Pratt
Chris Pratt

Reputation: 239290

The only way the image would be requested at /blog/images/header.png is if {{ STATIC_URL }} is evaluated to an empty string. The only way that would happen is if it's not set in the context, and the only way that would happen is if you don't have the staticfiles template context processor included:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.static',
)

Upvotes: 0

yedpodtrzitko
yedpodtrzitko

Reputation: 9359

seems like you miss following things:

settings.py (maybe syncdb is needed)

INSTALLED_APPS = (
...
"django.contrib.staticfiles",
...
)

TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.static',
...
)

main urls.py:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns += staticfiles_urlpatterns()

run collectstatic command

also there's doc page with all details:

https://docs.djangoproject.com/en/1.3/howto/static-files/

Upvotes: 0

Related Questions