0

我升级到 Django 1.4,现在我的管理静态文件将无法加载。

我的设置文件的相关部分如下所示:

MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'

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

INSTALLED_APPS = (
    # django apps
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.messages',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.staticfiles',

我收集了。

所以现在文件显示在我项目的静态目录中,但是当我尝试访问时,例如,http://localhost:8000/static/admin/js/actions.js我得到一个带有“/usr/local/”的 404 lib/python2.7/dist-packages/django/contrib/admin/media/js/actions.js" 不存在

为什么 django 在我的媒体目录而不是我的静态目录中查找,我该如何解决?

4

2 回答 2

1

问题实际上是我使用的是runcserver而不是runserver,并且runcserver已经硬编码:

option_list = BaseCommand.option_list + (
    make_option('--noreload', action='store_false', dest='use_reloader', default=True,
        help='Tells Django to NOT use the auto-reloader.'),
    make_option('--adminmedia', dest='admin_media_path', default='',
        help='Specifies the directory from which to serve admin media.'),
)

try:
    path = admin_media_path or django.__path__[0] + '/contrib/admin/media'

而不是'/contrib/admin/static'

使用 runcserver --adminmedia 'static/admin' 清除一切。

于 2012-04-17T22:17:59.507 回答
0

来自https://docs.djangoproject.com/en/dev/releases/1.4/#django-contrib-admin

如果您隐式依赖 Django 源代码中管理静态文件的路径,则需要更新该路径。文件django/contrib/admin/media/ 移动到 django/contrib/admin/static/admin/。

检查你的 urls.py。也许它包含这样的东西:

if settings.DEBUG:
    urlpatterns += patterns('',
    url(r'^somepath/(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'),
)
于 2012-04-17T22:12:27.897 回答