Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django won't serve static files while using development server

I just started a new development server for a website I am working on and I can't seem to get the Django development server to serve the static files I have for CSS and other things. The CSS for the admin site loads fine. I am running it in a virtualenv sandbox.

In settings.py I've messed around with MEDIA_ROOT and MEDIA_URL.

So far for MEDIA_ROOT I've tried.

MEDIA_ROOT = '/home/wluw/wluw/wluw/media'

and

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media')

I changed my ADMIN_MEDIA_PREFIX to

ADMIN_MEDIA_PREFIX = '/admin_media/'

my MEDIA_URL looks like this

MEDIA_URL = '/media/'

and the urls.py section for the static files looks like this.

if settings.DEBUG:
urlpatterns += patterns('',
     (r'^media/(?P<path>.*)$', 'django.views.static.serve',         
     {'document_root': settings.MEDIA_ROOT}),
)

Here's the output from the dev server when I try to access the page.

[21/Jul/2011 21:19:25] "GET /media/css/style.css HTTP/1.1" 302 0
[21/Jul/2011 21:19:25] "GET /media/css/style.css/ HTTP/1.1" 404 2561



from django.conf.urls.defaults import patterns, include, handler500, handler404
from django.conf import settings
from django.contrib import admin
import d51_django_admin_piston

handler500 = 'radio.frontend.views.server_error'

admin.autodiscover()
d51_django_admin_piston.autodiscover(admin.site)

urlpatterns = patterns(
'',
(r'^logs/', include('radio.logs.urls')),
(r'^events/', include('radio.events.urls')),
(r'^station/', include('radio.station.urls')),
(r'^staff/', include('radio.staff.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^accounts/login/$', 'django.contrib.auth.views.login'),
(r'^', include('radio.frontend.urls')),
)

if settings.DEBUG:
urlpatterns += patterns('',
     (r'^media/(?P<path>.*)$', 'django.views.static.serve',         
    # {'document_root': settings.MEDIA_ROOT}),
    {'document_root': settings.MEDIA_ROOT, 'show_indexes': True})
)

Here is my radio.frontend.urls

from django.conf.urls.defaults import *


urlpatterns = patterns('radio.frontend.views',
    url(r'^$', 'home', name='home'),
)

Here is my settings.py settings.py

Everything was working fine on the production server having /media? being the url for css and other things.

Also none of the content in the database is being shown. Each page of the site is created with a base.html and a viewname.html. Only the base.html part is showing up. I am sure this is a topic for an other question though.

I've looked at a ton of other posts with people having the same problem and none of them provided a solution. I am completely stumped.

Any help would be greatly appreciated. Thanks

like image 482
thebeagle Avatar asked Jul 21 '11 23:07

thebeagle


1 Answers

In Django 1.3 MEDIA_ROOT and MEDIA_URL are used to configure the physical location for user-uploads.

For static files you should use STATIC_URL:

STATIC_URL = '/static/' # URL prefix for static files.

and STATICFILES_DIRS:

PROJECT_DIR = os.path.dirname(__file__)

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_DIR, 'static'),
)

also make sure that you have STATICFILES_FINDERS configured.

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

In your templates you can use the STATIC_URL variable to access the location of your static files:

<link href="{{ STATIC_URL }}css/style.css" rel="stylesheet" type="text/css" />

That should be enough for the development env./server. No need to configure anything in urls.py.

For more information you can visit official django doc site describing how to manage static files: https://docs.djangoproject.com/en/dev/howto/static-files/

like image 50
tehshin Avatar answered Nov 14 '22 23:11

tehshin