Django site_media相对url问题
在我的 settings.py 中,我有以下内容:
PROJECT_DIR = os.path.dirname(os.path.realpath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_DIR,'templates')
MEDIA_URL = '/templates/'
在 urls.py 中,我有 我的 base.html 有以下指令: 首次进入应用程序时(即 http://localhost) 这个样式表加载得很好。但是,在后续的 http 请求中(在 urls.py 中,它是 样式表 http://localhost/assist/bat/site_media /bat/design/css/bat.css 未加载,因为其 MIME 类型“text/html”不是“text/css”。 正如你所看到的,这个 css 指令变成了一个完全不正确的相对 URL。如果我从该网址中删除 /assist/bat ,那么它就可以正常工作。那么我怎样才能将我的应用程序设置为不以这种方式变形 url 呢? 谢谢, 伊戈尔(r'^site_media/(?P
<link media="screen" href="site_media/bat/design/css/bat.css" type="text/css" rel="stylesheet" />
(r'^assist/bat/', include('assist.bat.urls'))
,到另一个模板,此指令会导致出现以下错误:
我的 base.html 有以下指令:
<link media="screen" href="site_media/bat/design/css/bat.css" type="text/css" rel="stylesheet" />
首次进入应用程序时(即 http://localhost) 这个样式表加载得很好。但是,在后续的 http 请求中(在 urls.py 中,它是 (r'^assist/bat/', include('assist.bat.urls'))
,到另一个模板,此指令会导致出现以下错误:
样式表 http://localhost/assist/bat/site_media /bat/design/css/bat.css 未加载,因为其 MIME 类型“text/html”不是“text/css”。
正如你所看到的,这个 css 指令变成了一个完全不正确的相对 URL。如果我从该网址中删除 /assist/bat ,那么它就可以正常工作。那么我怎样才能将我的应用程序设置为不以这种方式变形 url 呢?
谢谢, 伊戈尔
in my settings.py I have the following:
PROJECT_DIR = os.path.dirname(os.path.realpath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_DIR,'templates')
MEDIA_URL = '/templates/'
In urls.py I have (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
And my base.html has the following directive:
<link media="screen" href="site_media/bat/design/css/bat.css" type="text/css" rel="stylesheet" />
Upon first entry into the application (i.e. http://localhost) this stylesheet gets loaded just fine. However, on a subsequent http request (in urls.py it is (r'^assist/bat/', include('assist.bat.urls'))
, to another template this directive results in the following error:
The stylesheet http://localhost/assist/bat/site_media/bat/design/css/bat.css was not loaded because its MIME type, "text/html", is not "text/css."
As you can see, this css directive gets morphed into a relative url which is completely incorrect. If I remove /assist/bat from that url, then it works just fine. So how can I set up my app to not morph url's this way?
Thanks,
Igor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
疯狂猜测:
href="site_media/bat/design/css/bat.css"
应该是href="/site_media/bat/design/css/bat.css"
,前面不带斜杠的 url 会相对于当前 url 进行解析。Wild guess:
href="site_media/bat/design/css/bat.css"
should behref="/site_media/bat/design/css/bat.css"
, urls starting without slash in front of them are resolved relatively to the current url.