开发者

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_me开发者_JS百科dia/'

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


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/


You are trying to access your static files via '/static/' instead of '/media/' since in your comments you say:

"GET /static/css/style.css HTTP/1.1" 302 0 [21/Jul/2011 21:13:31] "GET /static/css/style.css/ HTTP/1.1" 404

Either you access it that way:

"GET /media/css/style.css HTTP/1.1" 302 0 [21/Jul/2011 21:13:31] "GET /media/css/style.css/ HTTP/1.1" 404

And you set you URL in your templates accordingly.

Or, you setup your routing this way:

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

I'll choose the second one and would setup settings.STATIC_FILE as you usually use MEDIA_ROOT for upload/download content.


The problem must be in your urls.py, since the AppendSlashMiddleware - which is what is causing the redirect to the URL ending in a slash - only kicks in if the URL is not matched at all, and from what you've shown it should match.

Silly question, but are you sure DEBUG is True? Can you show the rest of the urls.py? Are you sure that's the main urls.py, not one that's included by another?

Edit OK, it's hard to tell without seeing your radio.frontend.urls file, but it looks like what's happening is that all the URLs are being matched against that - because you use r'^' to include it, which matches everything, aomething in that file is also being too general. You might want to break up the first urlpattern, and put the include after the static bit.


For the development server

  1. Make a static folder in the django root
  2. Add this to the STATIC_DIRS in settings.py ('assets','path to the static folder')
  3. Put the Resources in the respective folders in the static folder you had created earlier
  4. Then run python manage.py collectstatic. This will create an admin and an assets folder in the django root with the assets you have put
  5. In the Templates add {% load static %} at the top
  6. For the link use {% static 'assets/path_to_resources_as_added_in_the_static_folder' %}

This works for me

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜