gpt4 book ai didi

python - 如何在django中检测当前域名?

转载 作者:太空宇宙 更新时间:2023-11-03 15:27:02 24 4
gpt4 key购买 nike

我想设计我的 Web 应用程序来根据用户输入我的 django 应用程序的域来提供两个不同的前端模板。

因此,如果用户输入 aaa.com,它将从应用程序 AAA_USA 提供前端服务,但如果用户从 aaa.co.my 输入,它将从应用程序 AAA_MY 提供前端服务。

执行此操作的最佳方法是什么?我正在考虑“检测当前域名”,然后在 View 函数中添加一条 if-else 语句。

这两个域将指向包含我的 Django 应用程序的相同名称服务器。

最佳答案

我的做法基本上是使用中间件(使用 session 并检测 HTTP_HOST。

class SimpleMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.

def __call__(self, request):
# Code to be executed for each request before the view (and later middleware) are called.

# sets to show Taiwan or Indo version
# sets the timezone too
http_host = request.META['HTTP_HOST']
if(http_host == 'http://www.xxx.tw'):
request.session['web_to_show'] = settings.TAIWAN
request.session['timezone_to_use'] = settings.TAIWAN_TIMEZONE
else:
request.session['web_to_show'] = settings.INDO
request.session['timezone_to_use'] = settings.INDONESIA_TIMEZONE

response = self.get_response(request)

# Code to be executed for each request/response after the view is called.

return response

关于python - 如何在django中检测当前域名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43060715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com