Django won't serve static files while using development server

此生再无相见时 提交于 2019-12-05 14:55:45

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.

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/

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!