Django获取静态文件的绝对URL [英] Django Get absolute url for static files

查看:313
本文介绍了Django获取静态文件的绝对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中,当我使用:

In Django, when I use:

{{ request.build_absolute_uri }}{% static "img/myimage.jpg" %}

它产生:' http://myurl.com//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。这意味着如果你在例如' http://myurl.com/login/ ',结果完整的网址将是' http://myurl.com/login//static/img/myimage.jpg '。

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()(可选地连同请求。方案),或者最好使用 sites framework 将模板变量设置为当前站点域。 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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