Django Admin Media 前缀 URL 问题
我有以下文件夹结构
src\BAT\templates\admin\base.html
src\BAT\media\base.css
src\BAT\media\admin-media\base.css
settings.py
MEDIA_ROOT = os.path.join( APP_DIR, 'media' )
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/admin-media/'
TEMPLATE_DIRS = (
os.path.join( APP_DIR, 'templates' )
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.admindocs',
)
urls.py
urlpatterns = patterns('',
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
我需要在我的应用程序中获取这两个 CSS 文件。我的 base.html 包含
<head>
<title>{% block title %}{% endblock %}</title>
<link href="{{ MEDIA_URL }}css/base.css" rel="stylesheet" type="text/css" />
<link href="{{ MEDIA_URL }}{{ADMIN_MEDIA_PREFIX}}css/base.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/base.css{% endblock %}" />
{% block extrastyle %}{% endblock %}
<!--[if lte IE 7]><link rel="stylesheet" type="text/css" href="{% block stylesheet_ie %}{% load adminmedia %}{% admin_media_prefix %}css/ie.css{% endblock %}" /><![endif]-->
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% admin_media_prefix %}css/rtl.css{% endblock %}" />{% endif %}
<script type="text/javascript">window.__admin_media_prefix__ = "{% filter escapejs %}{% admin_media_prefix %}{% endfilter %}";</script>
{% block extrahead %}{% endblock %}
{% block blockbots %}<meta name="robots" content="NONE,NOARCHIVE" />{% endblock %}
</head>
我想获得以下 URL 输出 http://localhost:8000/admin
<head>
<title>Site administration | My site admin</title>
<link href="/media/css/base.css" rel="stylesheet" type="text/css" />
<link href="/media/admin-media/css/base.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="/media/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="/media/admin/css/dashboard.css" />
但我总是
<head>
<title>Site administration | My site admin</title>
<link href="/media/css/base.css" rel="stylesheet" type="text/css" />
<link href="/media/css/base.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="/admin-media/css/base.css" />
<link rel="stylesheet" type="text/css" href="/admin-media/css/dashboard.css" />
在直接访问 http://localhost:8000/admin- 显示来自 Python site-packages/django/contrib/admin/media/css 的 css 文件
media/css/base.css直接访问 http://localhost:8000/media/admin-media/css 时 /base.css 显示 src/media/admin-media/css/ 中的 css 文件,
而直接访问 http://localhost:8000/media/css/base.css
显示 css 文件来自 src/media/css/
i 've the following folder structure
src\BAT\templates\admin\base.html
src\BAT\media\base.css
src\BAT\media\admin-media\base.css
settings.py
MEDIA_ROOT = os.path.join( APP_DIR, 'media' )
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/admin-media/'
TEMPLATE_DIRS = (
os.path.join( APP_DIR, 'templates' )
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.admindocs',
)
urls.py
urlpatterns = patterns('',
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^media/(?P<path>.*)
I need to get both the CSS files in my application. my base.html contains
<head>
<title>{% block title %}{% endblock %}</title>
<link href="{{ MEDIA_URL }}css/base.css" rel="stylesheet" type="text/css" />
<link href="{{ MEDIA_URL }}{{ADMIN_MEDIA_PREFIX}}css/base.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/base.css{% endblock %}" />
{% block extrastyle %}{% endblock %}
<!--[if lte IE 7]><link rel="stylesheet" type="text/css" href="{% block stylesheet_ie %}{% load adminmedia %}{% admin_media_prefix %}css/ie.css{% endblock %}" /><![endif]-->
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% admin_media_prefix %}css/rtl.css{% endblock %}" />{% endif %}
<script type="text/javascript">window.__admin_media_prefix__ = "{% filter escapejs %}{% admin_media_prefix %}{% endfilter %}";</script>
{% block extrahead %}{% endblock %}
{% block blockbots %}<meta name="robots" content="NONE,NOARCHIVE" />{% endblock %}
</head>
I want to get the following output for URL http://localhost:8000/admin
<head>
<title>Site administration | My site admin</title>
<link href="/media/css/base.css" rel="stylesheet" type="text/css" />
<link href="/media/admin-media/css/base.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="/media/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="/media/admin/css/dashboard.css" />
But I always getting
<head>
<title>Site administration | My site admin</title>
<link href="/media/css/base.css" rel="stylesheet" type="text/css" />
<link href="/media/css/base.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="/admin-media/css/base.css" />
<link rel="stylesheet" type="text/css" href="/admin-media/css/dashboard.css" />
while direct accessing http://localhost:8000/admin-media/css/base.css
shows css file from Python site-packages/django/contrib/admin/media/css
while direct accessing http://localhost:8000/media/admin-media/css/base.css
shows css file from src/media/admin-media/css/
while direct accessing http://localhost:8000/media/css/base.css
shows css file from src/media/css/
, 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
I need to get both the CSS files in my application. my base.html contains
I want to get the following output for URL http://localhost:8000/admin
But I always getting
while direct accessing http://localhost:8000/admin-media/css/base.css
shows css file from Python site-packages/django/contrib/admin/media/css
while direct accessing http://localhost:8000/media/admin-media/css/base.css
shows css file from src/media/admin-media/css/
while direct accessing http://localhost:8000/media/css/base.css
shows css file from src/media/css/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 Django 1.4 及更高版本很重要(请参阅此处) :
之前的答案,对于较旧的 Django 版本:
ADMIN_MEDIA_PREFIX
是一个绝对 URL 前缀,它与
MEDIA_URL
无关 - 两者都可以指向完全不同的点。诚然,名称中“_PREFIX”的(错误)选择在某种程度上表明了这一点。因此,必须是
{% admin_media_prefix %}css/base.css
,而不是{{ MEDIA_URL }}{{ADMIN_MEDIA_PREFIX}}css/base.css
。然后您必须确保 Web 服务器在“/admin-media/”上提供管理媒体文件。请注意,我在上面使用了
admin_media_prefix
标签,该标签需要在模板的开头处使用{% load adminmedia %}
。不幸的是,常规媒体上下文处理器只为您提供 MEDIA_URL 变量。为了覆盖普通管理媒体服务,请在 URLconf 中尝试以下操作:
Important for Django 1.4 and newer (see here):
Previous answer, for older Django releases:
ADMIN_MEDIA_PREFIX
is meant to be an absolute URL prefix, it has nothing to do with theMEDIA_URL
- both can point to completely different points. Admittedly, the (bad) choice of "_PREFIX" in the name somewhat suggests that.So, instead of
{{ MEDIA_URL }}{{ADMIN_MEDIA_PREFIX}}css/base.css
it must be{% admin_media_prefix %}css/base.css
. And then you have to ensure that the web server serves the admin media files on '/admin-media/'.Note that I used the
admin_media_prefix
tag above, which needs{% load adminmedia %}
at the beginning of the template. The regular media context processor only gives you the MEDIA_URL variable, unfortunately.In order to override the vanilla admin media serving, try something like this in your URLconf:
Django 1.4 使用新的策略来加载静态媒体文件,使用它的人需要阅读 https://docs.djangoproject.com/en/dev/howto/static-files/
上面链接的执行摘要是两个新的设置变量 STATIC_URL 和 STATIC_ROOT 与新的设置一起使用包含的应用程序(django.contrib.staticfiles)用于收集和提供基于每个应用程序包含的静态文件。
当升级我的 django 安装时,我必须将 STATIC_ROOT 设置为等于我以前的 MEDIA_URL。
在此系统下,模板现在应使用 {{ STATIC_URL }}。
Django 1.4 uses a new strategy for loading static media files, those using it will want to read over https://docs.djangoproject.com/en/dev/howto/static-files/
The executive summary of the above link is that two new settings variables, STATIC_URL and STATIC_ROOT, are used together with a newly included app (django.contrib.staticfiles) to collect and serve static files which are included on a per app basis.
When upgrading my django installation I had to set my STATIC_ROOT equal to my previous MEDIA_URL.
Under this system templates should now use {{ STATIC_URL }}.