问题描述
在Django中,当我使用:
In Django, when I use:
{{ request.build_absolute_uri }}{% static "img/myimage.jpg" %}
它产生:''。这会产生错误。
It produces: 'http://myurl.com//static/img/myimage.jpg'. This produces an error.
如何删除双斜杠?
STATIC URL是:
The STATIC URL is:
STATIC_URL = '/static/'
但是我不认为删除第一个'/'将是一个好主意。
But I don't think removing the first '/' would be a good idea.
推荐答案
build_absolute_uri
方法构建当前页面的绝对uri。这意味着如果你在例如'',结果完整的网址将是' '。
The build_absolute_uri
method builds an absolute uri for the current page. That means that if you're on e.g. 'http://myurl.com/login/', the resulted full url would be 'http://myurl.com/login//static/img/myimage.jpg'.
而是使用 request.get_host()
(可选地连同请求。方案
),或者最好使用将模板变量设置为当前站点域。 get_host()
方法具有。
Instead, use request.get_host()
(optionally together with request.scheme
for the url scheme), or preferably, use the sites framework to set a template variable to the current site domain. The get_host()
method has some issues regarding proxies.
get_host()
方法将返回当前域,而不添加路径。
The get_host()
method will return the current domain without a path appended.
这篇关于Django获取静态文件的绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!