Python 如何从 django 请求中获取完整的 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26989078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get full url from django request
提问by David542
Is there a better way to get the full url in django than doing the following:
有没有比执行以下操作更好的方法来获取 django 中的完整 url:
url = request.META['HTTP_HOST']
+ request.META['PATH_INFO']
+ request.META['QUERY_STRING']
Is there something like request.META['URL']?
有类似的东西request.META['URL']吗?
采纳答案by Yogesh dwivedi Geitpl
You can get full URL using request.build_absolute_urimethod:
您可以使用以下request.build_absolute_uri方法获取完整 URL :
FULL_URL_WITH_QUERY_STRING: request.build_absolute_uri()
FULL_URL: request.build_absolute_uri('?')
ABSOLUTE_ROOT: request.build_absolute_uri('/')[:-1].strip("/")
ABSOLUTE_ROOT_URL: request.build_absolute_uri('/').strip("/")
Should this will help full to you.
如果这对你有帮助。
The best way to use ABSOLUTE URLS in Django, you can create a context_processors or middleware and find your ABSOLUTE_URLand return that so that you can use any where in Django.
在 Django 中使用绝对 URL 的最佳方法是,您可以创建一个 context_processors 或中间件,然后找到ABSOLUTE_URL并返回它,以便您可以在 Django 中的任何位置使用。
Like this example:
像这个例子:
def absolute(request):
urls = {
'ABSOLUTE_ROOT': request.build_absolute_uri('/')[:-1].strip("/"),
'ABSOLUTE_ROOT_URL': request.build_absolute_uri('/').strip("/"),
}
return urls
And Then you should use {{ABSOLUTE_ROOT}}in any where into you django template.
然后你应该{{ABSOLUTE_ROOT}}在任何地方使用你的 django 模板。
回答by David542
回答by C.K.
you can use
您可以使用
in template
在模板中
{{ request.META.PATH_INFO }}
in views
在视图中
end_point = request.META.get('PATH_INFO', None)

