我设置了一个简单的项目,一切都工作正常到一定程度。我在项目中添加了东西,静态目录的路径等,然后突然我意识到管理媒体停止服务,没有css,没有图像。
查看页面来源显示:
link rel="stylesheet" type="text/css" href="/static/admin/css/base.css"
哪个是正确的,并且相同的路径适用于任何新创建的Django项目。
基本上我想以某种方式重新启用管理媒体从默认位置提供服务。
这是settings.py但坦率地说,我从它停止工作的那一点起并没有改变它。
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, "site_media", "media")
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = "/site_media/media/"
# Absolute path to the directory that holds static files like app media.
# Example: "/home/media/media.lawrence.com/apps/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static1")
# URL that handles the static files like app media.
# Example: "http://media.lawrence.com"
STATIC_URL = "/site_media/"
# Additional directories which hold static files
STATICFILES_DIRS = [
os.path.join(PROJECT_ROOT, "site_media"),
os.path.join(PROJECT_ROOT, "site_media", "static"),
os.path.join(PROJECT_ROOT, "site_media", "media"),
]
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/static/admin/'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '__@9nw29=7gbj8xb5z*u6cew3x8m(&_v&jlp16!^bnpe+6@w0#'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'wizs.urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_ROOT, "templates"),
)
TEMPLATE_CONTEXT_PROCESSORS = [
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
'django.core.context_processors.static',
]
的更新 的
似乎是为了解决这两个属性:
1)现在我看到管理媒体但不是媒体(例如上传的图片)
STATIC_URL = "/site_media/static/"
ADMIN_MEDIA_PREFIX = '/site_media/static/admin/'
2)现在我看到上传的媒体文件,但不是管理媒体
STATIC_URL = "/site_media/"
ADMIN_MEDIA_PREFIX = '/site_media/static/admin/'
答案 0 :(得分:1)
看起来STATIC_URL应该是“/ site_media / static1 /”而ADMIN_MEDIA_PREFIX应该是“/ site_media / static1 / admin /”
其他一些建议:
os.path.join(PROJECT_ROOT, "site_media", "media"),
os.path.join(PROJECT_ROOT, "site_media", "static"),
并将其更改为os.path.join(PROJECT_ROOT, "static"),
并将该目录移到那里。以下是我的设置:
# settings.py
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'site_media', 'media')
MEDIA_URL = '/site_media/media/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'site_media', 'static')
STATIC_URL = '/site_media/static/'
ADMIN_MEDIA_PREFIX = '/site_media/static/admin/'
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
# urls.py
from django.conf import settings
urlpatterns += patterns('',
url(r'^site_media/static/(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'),
url(r'^site_media/media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
})
)