How to get hostname or IP in settings.py so that i can use it to decide which app's urls to use

旧时模样 提交于 2019-12-06 16:13:03

You need two alternative urlconf files in your main project:

# project/project/urls_id.py

from django.conf.urls import url

from urls import urlpatterns

urlpatterns.append(url(r'^', include 'webname_id.urls'))
# project/project/urls_uk.py

from django.conf.urls import url

from urls import urlpatterns

urlpatterns.append(url(r'^', include 'webname_uk.urls'))

In your middleware, select the appropriate urlconf based on the host.

class YourMiddleware(object):

    # For Django 1.10+

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):

        if request.get_host() == 'www.webname.co.id':
            request.urlconf = 'project.urls_id'
        else:
            request.urlconf = 'project.urls_uk'

        response = self.get_response(request)

        return response
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!