Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django中检测当前域名?_Python_Django - Fatal编程技术网

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

Python 如何在django中检测当前域名?,python,django,Python,Django,我想根据用户进入django应用程序的域,将我的web应用程序设计为两个不同的前端模板 因此,如果用户输入aaa.com,它将从应用aaa_USA提供前端服务,但如果用户从aaa.co.my输入,它将从应用aaa_my提供前端服务 最好的方法是什么?我想“检测当前域名”,然后简单地在视图函数中添加if-else语句 这两个域将指向包含我的Django应用程序的相同名称服务器。使用 request.build_absolute_uri() 将检索完整路径: es: 我这样做的方式基本上是使用中

我想根据用户进入django应用程序的域,将我的web应用程序设计为两个不同的前端模板

因此,如果用户输入aaa.com,它将从应用aaa_USA提供前端服务,但如果用户从aaa.co.my输入,它将从应用aaa_my提供前端服务

最好的方法是什么?我想“检测当前域名”,然后简单地在视图函数中添加if-else语句

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

使用

request.build_absolute_uri()
将检索完整路径: es:


我这样做的方式基本上是使用中间件(使用会话和检测HTTP_主机)

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

如果您无权访问requests对象,则可以使用:


这将返回Django配置为的站点(使用保存在数据库Django_site表中的值)(请参阅:)

使用{request.get_host}谢谢,但是没有request对象的情况下是否仍可以执行此操作?因为我可能还需要在视图函数(包含request参数)之外使用它检查此项:Site.objects.get_current().domain可能的重复项是否有不使用站点包的方法?如果没有,谢谢,我将尝试一下,看看是否可行。是否仍有不使用请求对象的方法?因为我可能还需要在视图函数(包含请求参数)之外使用它没有请求对象是不可能的。2种方法1)在其他函数中作为参数传递请求对象2)在第一个视图中,将路径值保存在cookie中,并在其他函数中读取cookie
from django.contrib.sites.shortcuts import get_current_site
domain = get_current_site(None)