1

I'm stuck due to an evergreen issue, static files not served. Conversely the files placed in the MEDIA_ROOT subtree get served correctly under MEDIA_URL.

Stripped settings.py:

DEBUG = True
STATIC_URL = '/static/'
STATIC_ROOT = '/home/foo/devel/static'
MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/foo/devel/media'
# the following is deprecated but is it seems grappelly requires it
ADMIN_MEDIA_PREFIX = STATIC_URL + "grappelli/"
STATIC_FILES = ()
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

To create the project I did:

$ cd /home/foo/devel/
$ virtualenv testdrive
$ . bin/activate; pip install django; cd testdrive
$ django-admin.py fbtest

and got this directory tree (stripped):

. <-- /home/foo/devel/
├── bin
├── fbtest
│   └── fbtest
│       ├── media
│       │   └── foo.jpg
│       ├── static
│       └────── foo.jpg
├── include
└── lib

Files under STATIC_URL should be served automatically by Django staticfiles (not in my case), while other files have to be handled manually. So I appended these lines to urls.py:

import settings
if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL.lstrip("/"),
            'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
    )

Accessing http://host/media/filebrowser/foo.jpg works, while http://host/static/foo.jpg gives error 404. Why?

4

3 回答 3

6

下面的文件STATIC_URL应该由 Django staticfiles 自动提供(在我的情况下不是),而其他文件必须手动处理。

这是不正确的。Django 从来没有服务STATIC_ROOT过——甚至在开发中也没有。它所做的是在每个应用程序的“静态”目录中创建文件,并在 .available 中指定的任何目录中创建STATICFILES_DIRS文件STATIC_URL。您实际上并没有手动放入任何东西STATIC_ROOT。事实上,在开发中,您甚至不应该在那里拥有目录。简而言之,当您运行管理命令时,它只是生产中STATIC_ROOT静态文件的垃圾场。collectstatic

在开发中,所有静态文件都应该进入someapp/static,其中“someapp”是它们应用的应用程序。如果文件作为一个整体应用于项目,例如全局 CSS 文件,您需要创建一个完全不同的目录(即与orSTATIC_ROOT不同MEDIA_ROOT),然后将该目录添加到STATICFILES_DIRS. 例如,我通常称我的“资产”,所以:

STATICFILES_DIRS = (
    os.path.join(os.path.dirname(__file__), 'assets'),
)
于 2012-06-12T21:42:12.457 回答
1

这是一个愚蠢的错误。我忘了添加fbtestINSTALLED_APPS,所以静态文件机制没有管理这个应用程序的静态文件。

于 2012-06-12T21:42:41.567 回答
0

这个问题真的是常青树......一些提示:

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

INSTALLED_APPS = (
    # ...
    'django.contrib.staticfiles',
    # ... 
)

我对 django 1.4 的设置(没有 grappelli):

网址.py

if settings.DEBUG:
    urlpatterns = patterns('',

        url(r'^%s(?P<path>.*)$' % settings.STATIC_URL.lstrip('/'), 'django.views.static.serve',
            {'document_root': settings.STATIC_ROOT, "show_indexes": True}),

        url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL.lstrip('/'), 'django.views.static.serve',
            {'document_root': settings.MEDIA_ROOT, "show_indexes": True}),

    ) + urlpatterns

设置.py

MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'


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

INSTALLED_APPS = (
    # ...
    # 'django.contrib.staticfiles',
    # ...
)
于 2012-06-12T20:33:23.373 回答