如何从 Django 中的请求对象获取 URI?

发布于 2024-08-30 01:10:11 字数 56 浏览 3 评论 0原文

如何从 Django 中的请求对象获取 URI?

有 request.uri 吗?

How do you get the URI from request object in Django?

Is there request.uri?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞笑我太脆弱 2024-09-06 01:10:11
request.META['REQUEST_URI']

或者

request.get_full_path()

你往往会产生大量琐碎的问题,你可以在文档/谷歌中轻松找到答案......

request.META['REQUEST_URI']

or

request.get_full_path()

You tend to generate a flood of trivial questions, answers to which you could easily find in documentation/Google...

爺獨霸怡葒院 2024-09-06 01:10:11

在 django 3.2 中,没有 request.META['REQUEST_URI'] ,如上面的答案所示。

最简单的方法是打印元数据,并找到您需要的内容:

print(request.META)

这就是我解决此问题的方法:

例如请求的网址将是:

https://example.com/first_folder/nice_page/

Get domain - (example.com)

domain = request.META['HTTP_HOST']
# its will output:
# example.com 

获取路径 - /first_folder/nice_page/

path = request.META['PATH_INFO']
# or 
path = request.get_full_path()
# both of this options will have the same output:
# /first_folder/nice_page/

获取协议 - http://https://

protocol = request.META['wsgi.url_scheme']
# this will output:
# http   or   https

所以你的完整路径可能是这样的:

protocol = request.META['wsgi.url_scheme']    
domain = request.META['HTTP_HOST']
path = request.META['PATH_INFO']

full_path = protocol + "://" + domain + path

# it's output will be:
# https://example.com/first_folder/nice_page/

In django 3.2 there is no request.META['REQUEST_URI'] as it is in answer above.

Easiest way is to print Meta data, and find what you need:

print(request.META)

This is how i solved this problem:

For example requested url will be:

https://example.com/first_folder/nice_page/

Get domain - (example.com):

domain = request.META['HTTP_HOST']
# its will output:
# example.com 

Get path - /first_folder/nice_page/:

path = request.META['PATH_INFO']
# or 
path = request.get_full_path()
# both of this options will have the same output:
# /first_folder/nice_page/

Get protocol - http:// or https://

protocol = request.META['wsgi.url_scheme']
# this will output:
# http   or   https

So your full path may be like this:

protocol = request.META['wsgi.url_scheme']    
domain = request.META['HTTP_HOST']
path = request.META['PATH_INFO']

full_path = protocol + "://" + domain + path

# it's output will be:
# https://example.com/first_folder/nice_page/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文