Django:提供管理媒体文件 [英] Django: serving ADMIN media files
问题描述
我已经成功地为普通媒体文件提供媒体文件,但是当我尝试提供管理媒体文件时,我失败了.请帮助我找到问题所在,因为我已经尝试解决问题几个小时了,但都没有成功(我也在谷歌上搜索并阅读了关于提供静态文件的 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:提供管理媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!