如何在Django中获取完整的/绝对的URL(带域名)? [英] How can I get the full/absolute URL (with domain) in Django?

查看:1156
本文介绍了如何在Django中获取完整的/绝对的URL(带域名)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题,但是必须有一种方法可以在没有站点模块的情况下执行。这只是愚蠢的...我不需要查询我的数据库来阻止URL!

Exactly the same as this question, but there must be a way to do it without the Sites module. That's just silly... I shouldn't need to query my DB to snag the URL!

我想使用它与 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屋!

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