如何在django视图函数中获取域名? [重复]

时间:2022-03-07 23:45:39

This question already has an answer here:

这个问题在这里已有答案:

My application is going to be hosted in multiple domains, I need a domain name in django view function...How to achieve this? I am using django 1.3.3.

我的应用程序将被托管在多个域中,我需要在django视图功能中使用域名...如何实现这一目标?我正在使用django 1.3.3。

I want something like this

我想要这样的东西

www.syz.com 127.0.0.1:8000

www.syz.com 127.0.0.1:8000

1 个解决方案

#1


0  

Have a look at request.get_host()

看看request.get_host()

Doc: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.get_host

Doc:https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.get_host

Or in your template (if you use a requestContext) {{request.get_host}}

或者在您的模板中(如果您使用requestContext){{request.get_host}}

as @jayanth-koushik mentionned it, you can find more here: How can I get the domain name of my site within a Django template?

正如@jayanth-koushik所提到的,你可以在这里找到更多:如何在Django模板中获取我的网站的域名?

#1


0  

Have a look at request.get_host()

看看request.get_host()

Doc: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.get_host

Doc:https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.get_host

Or in your template (if you use a requestContext) {{request.get_host}}

或者在您的模板中(如果您使用requestContext){{request.get_host}}

as @jayanth-koushik mentionned it, you can find more here: How can I get the domain name of my site within a Django template?

正如@jayanth-koushik所提到的,你可以在这里找到更多:如何在Django模板中获取我的网站的域名?