Django 管理媒体停止在本地提供服务

发布于 2024-12-10 04:34:15 字数 3171 浏览 1 评论 0原文

我建立了一个简单的项目,在某种程度上一切都运行良好。我正在向项目添加内容、静态目录的路径等,然后突然我意识到管理媒体停止提供服务,没有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2024-12-17 04:34:15

看起来 STATIC_URL 应该是“/site_media/static1/”,ADMIN_MEDIA_PREFIX 应该是“/site_media/static1/admin/”

其他一些建议:

  1. 不要将 MEDIA_ROOT 编译到 STATIC_ROOT 中,因此删除 os.path.join (PROJECT_ROOT, "site_media", "media"),
  2. 您的本地静态文件也不应该位于 site_media 中。因此,删除 os.path.join(PROJECT_ROOT, "site_media", "static"), 并将其更改为 os.path.join(PROJECT_ROOT, "static"), 并将该目录移到那里。
  3. 您应该在本地清除 site_media,只在服务器上填充它。 (或者,当然是您在本地上传的任何媒体)。但您根本不需要在本地运行collectstatic。
  4. 如果/一旦您执行了这些操作,您可以将“static1”更改回静态,然后通过 nginx 提供整个 /site_media/ 文件夹,然后该文件夹中将不会有任何重复项。

祝你好运。

这是我的设置:

# 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,
    })
)

It looks like STATIC_URL should be "/site_media/static1/" and ADMIN_MEDIA_PREFIX should be "/site_media/static1/admin/"

Some other suggestions:

  1. Don't compile your MEDIA_ROOT into your STATIC_ROOT, so remove os.path.join(PROJECT_ROOT, "site_media", "media"),
  2. Your local static files should also not be in site_media. so remove os.path.join(PROJECT_ROOT, "site_media", "static"), and change it to os.path.join(PROJECT_ROOT, "static"), and move that directory there.
  3. You should the clear out site_media locally, and only have it filled on the server. (or, of course with any media that you upload locally). But you don't need to run collectstatic locally at all.
  4. If/Once you do those things, you can change "static1" back to static, and then serve the entire /site_media/ folder via nginx, and then you won't have any duplicates in that folder.

Good luck.

Here Are my settings:

# 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,
    })
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文