Django 管理媒体停止在本地提供服务
我建立了一个简单的项目,在某种程度上一切都运行良好。我正在向项目添加内容、静态目录的路径等,然后突然我意识到管理媒体停止提供服务,没有 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/'
I set up a simple project and everything was working fine up to a certain point. I was adding stuff to the project, paths to static dirs, etc, and then suddenly I realised that admin media stopped being served, no css, no images.
Viewing the source of the page reveals this:
link rel="stylesheet" type="text/css" href="/static/admin/css/base.css"
Which is correct and the same path works with any newly created Django project.
Basically I would like to somehow reenable that the admin media is being served from default location.
This is the settings.py but frankly I wasn't changing it much from the point when it stopped working.
# 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',
]
UPDATE
Seems that it came down to juggling these two properties:
1) Now I see admin media but NOT the media (e.g. uploaded images)
STATIC_URL = "/site_media/static/"
ADMIN_MEDIA_PREFIX = '/site_media/static/admin/'
2) Now I see uploaded media files but NOT the admin media
STATIC_URL = "/site_media/"
ADMIN_MEDIA_PREFIX = '/site_media/static/admin/'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来 STATIC_URL 应该是“/site_media/static1/”,ADMIN_MEDIA_PREFIX 应该是“/site_media/static1/admin/”
其他一些建议:
祝你好运。
这是我的设置:
It looks like STATIC_URL should be "/site_media/static1/" and ADMIN_MEDIA_PREFIX should be "/site_media/static1/admin/"
Some other suggestions:
os.path.join(PROJECT_ROOT, "site_media", "media"),
os.path.join(PROJECT_ROOT, "site_media", "static"),
and change it toos.path.join(PROJECT_ROOT, "static"),
and move that directory there.Good luck.
Here Are my settings: