gpt4 book ai didi

python - Django - 每个域的 URLConf?

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:56 26 4
gpt4 key购买 nike

我的 Django 项目中有两个应用程序:一个面向公众的应用程序和一个管理应用程序(不是 Django 的内置管理站点)。我想让一个域名指向公共(public)站点,另一个域名指向管理站点。 (例如,/index.html 路由会根据域名指向不同应用程序中的 View 。)每个应用程序都有自己的 URLconf,它们都包含在主 URLconf 中。最好的方法是什么?

最佳答案

您可以使用自己的主机中间件

例子在: https://code.djangoproject.com/wiki/MultiHostMiddleware

设置.py

HOST_MIDDLEWARE_URLCONF_MAP = {
# Control Panel
"www.example.com": "webapp.sites.example.urls",
}

多主机.py

from django.conf import settings
from django.utils.cache import patch_vary_headers

class MultiHostMiddleware:

def process_request(self, request):
try:
request.META["LoadingStart"] = time.time()
host = request.META["HTTP_HOST"]
if host[-3:] == ":80":
host = host[:-3] # ignore default port number, if present
if settings.HOST_MIDDLEWARE_URLCONF_MAP.has_key(host):
request.urlconf = settings.HOST_MIDDLEWARE_URLCONF_MAP[host]
request.META["MultiHost"] = str(request.urlconf)
else:
request.META["MultiHost"] = str(settings.ROOT_URLCONF)

except KeyError:
pass # use default urlconf (settings.ROOT_URLCONF)

def process_response(self, request, response):
if request.META.has_key('MultiHost'):
response['MultiHost'] = request.META.get("MultiHost")

if request.META.has_key('LoadingStart'):
_loading_time = time.time() - int(request.META["LoadingStart"])
response['LoadingTime'] = "%.2fs" % ( _loading_time, )

if getattr(request, "urlconf", None):
patch_vary_headers(response, ('Host',))
return response

关于python - Django - 每个域的 URLConf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9474619/

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