如何在 Django 中获取完整/绝对 URL(带域)? [英] How can I get the full/absolute URL (with domain) in Django?
问题描述
如何在 Django 没有站点模块?这太愚蠢了......我不需要查询我的数据库来获取 URL!
How can I get the full/absolute URL (e.g. https://example.com/some/path
) in Django without the Sites module? That's just silly... I shouldn't need to query my DB to snag the URL!
我想将它与 reverse()
一起使用.
I want to use it with reverse()
.
推荐答案
使用方便的request.build_absolute_uri() 方法根据请求,将相对 url 传递给它,它会给你一个完整的.
Use handy request.build_absolute_uri() method on request, pass it the relative url and it'll give you full one.
默认情况下,返回的是 request.get_full_path()
的绝对 URL,但您可以将相对 URL 作为第一个参数传递给它,以将其转换为绝对 URL.
By default, the absolute URL for request.get_full_path()
is returned, but you can pass it a relative URL as the first argument to convert it to an absolute URL.
这篇关于如何在 Django 中获取完整/绝对 URL(带域)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!