Django:提供 ADMIN 媒体文件 [英] Django: serving ADMIN media files

查看:37
本文介绍了Django:提供 ADMIN 媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地为普通的 MEDIA 文件提供媒体文件,但是当我尝试提供管理媒体文件时,我失败了.请帮我找出问题所在,因为我已经尝试解决问题几个小时了,但没有运气(也一直在谷歌搜索并阅读了关于提供静态文件的 django 文档).

I've been successfully serving media files for the normal MEDIA files, but when I tried serving admin media files, I failed. please kindly help me locating the problem, as I've tried to troubleshoot the problem for several hours already with no luck (been googling too and read the django doc about serving static files as well).

我尝试访问 localhost:8000/media/a.gif 的错误如下:

The error as I tried to access localhost:8000/media/a.gif is as following:

找不到页面:f:python25libsite-packagesdjango/contrib/admin/mediaa.gif

Page not found: f:python25libsite-packagesdjango/contrib/admin/mediaa.gif

我将管理媒体文件放在名为media"的目录中,而将普通媒体文件放在名为static"的目录中.我也在 Windows 上.

I put the admin media files in directory named "media", while I put the normal media files in directory named "static". I'm on Windows, too.

这是我在 urls.py 中提供普通媒体文件的方式:

Here's how I serve the ordinary media files in urls.py:

# serve static files
from django.conf import settings
if settings.ENVIRONMENT==settings.ENV_DEVELOPMENT:
    urlpatterns += patterns("django.views",
        url(r"%s(?P<path>.*)$" % settings.MEDIA_URL[1:], "static.serve", {"document_root": settings.MEDIA_ROOT,})
    )

还有我的 settings.py(仅重要部分):

And my settings.py (only the important pieces):

import project_path
MEDIA_ROOT = project_path.MEDIA.replace('\','/')
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'
TEMPLATE_DIRS = (
    project_path.TEMPLATE.replace('\','/'),
)

还有我的 project_path.py:

And my project_path.py:

import sys
from os.path import dirname, join
ROOT = dirname(__file__)
APP = join(ROOT, "apps")
TEMPLATE = join(ROOT, "templates")
MEDIA = join(ROOT, "static")
ADMIN_MEDIA = join(ROOT, "media")

有什么提示吗?

或者至少请分享您如何提供您的管理媒体文件(不更改来自网络服务器的任何文件,但只能通过 django 源代码)

or maybe at least please share how do you serve your admin media files (without changing any files from the web server, but only via the django source code)

提前致谢:)

推荐答案

你的答案是,除非 ADMIN_MEDIA_PREFIX 明确设置域runserver 命令将从 contrib.admin 中提供管理媒体文件.

Your answer is that unless ADMIN_MEDIA_PREFIX explicitly sets a domain the runserver command will serve out the admin media files from contrib.admin.

我也被这种魔法 行为烧伤了.有一张票(Ticket #8336),但设计决定是为了方便和混乱一样.

I got burned by this magic behaviour, too. There was a ticket for this (Ticket #8336), but the design decision was to leave the convenience and confusion as it is.

因此,要使用 runserver 命令从您的目录中提供您的管理媒体(用于使用 grappelli 或任何您想使用的管理皮肤),您必须使用类似:

So to serve your admin media (for using grappelli or whatever admin skin you want to use) from your directories with the runserver command you have to use something like:

MEDIA_ROOT =  os.path.join(PROJECT_ROOT, 'media/')
ADMIN_MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'admin-media/')
MEDIA_URL = '/site-media/'
ADMIN_MEDIA_PREFIX = 'http:/localhost:8000/admin-media/'

我希望我在这里复活了正确的问题.提前道歉.

I hope I am resurrecting the correct question here. Apologies in advance.

这篇关于Django:提供 ADMIN 媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆