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

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

问题描述

如何在 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屋!

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