Serving different urlconfs in one Django project

My question is how to serve multiple urls.py files (e.g. urls1.py, urls2.py, etc.) in a single Django project.

I am using Win7 x64, django 1.4.1, python 2.7.3 and as server django dev-server tool.
I decided to use the method that I found in google from

http://effbot.org/zone/django-multihost.htm

I created a multihost.py file and placed it in the django middleware folder:

C:\python27\Lib\site-packages\django\middleware\multihost.py 

With the following code:

 from django.conf import settings from django.utils.cache import patch_vary_headers class MultiHostMiddleware: def process_request(self, request): try: host = request.META["HTTP_HOST"] if host[-3:] == ":80": host = host[:-3] # ignore default port number, if present request.urlconf = settings.HOST_MIDDLEWARE_URLCONF_MAP[host] except KeyError: pass # use default urlconf (settings.ROOT_URLCONF) def process_response(self, request, response): if getattr(request, "urlconf", None): patch_vary_headers(response, ('Host',)) return response 

Also in my project setup.py file, I added a mapping dictionary, as shown above:

 # File: settings.py HOST_MIDDLEWARE_URLCONF_MAP = { "mysite1.com": "urls1", #"mysite2.com": "urls2" } 

I have not yet implemented error handling as described above.

My hosts file includes the following:

 127.0.0.1 mysite1.com 

The structure of the project is as follows:

effbot django project project folder:

  + effbot --- settings.py --- view.py --- wsgi.py --- urls.py --- urls1.py + objex --- models.py --- views.py + static --- css --- images 

The templates folder is missing, because I do not serve html elements from files, they come from databsse (I doubt my problem is this).

Now the problem is this: when I go for the address

 mysite1.com 

in my browser with the django dev server running, I get 301 code from the server. And the browser shows the message "do not display the page."

Could you explain to me how to use the mentioned method? I am new to django and have not done any real projects yet. Just read the docs and ran several sites at home to find out how it works.

I expect urlconfs to be called depending on the incoming

 request.META["HTTP_HOST"] 

The goal is to serve different urlconfs for mysite1.com and mysite2.com
in one django project.

I think this should work somehow.

Thanks for any feedback.

EDIT:
After some research attempts, I found that I incorrectly connected my multyhost.py in the settings.
Fixed. But the same result nevertheless.

I also found out that my django dev server does not reflect in any way that it processes any requests from a browser (IE9), except when I do "http://127.0.0.1".
Maybe I need to try some production server for my task, like nginx?

+4
source share
1 answer

Your ROOT_URLCONF should be effbot.urls without .py , as you can see in the example in the documentation .

Additionally, HOST_MIDDLEWARE_URLCONF_MAP should reflect ROOT_URLCONF , so add "effbot". eg:

 HOST_MIDDLEWARE_URLCONF_MAP = { "mysite1.com": "effbot.urls1", #"mysite2.com": "effbot.urls2" } 

One more thing, please try with a different browser (Chrome, Firefox), sometimes I had problems accessing the dev server from IE.

+1
source

All Articles