multiple domains - one django project
<p>I want to run ONE django project on multiple domains/websites. The websites each need to access a unique "urls.py"/"views.py". I tried it already with <a href="http://michal.karzynski.pl/blog/2010/10/19/run-multiple-websites-one-django-project/" rel="nofollow">this tutorial</a>, but it doesn't work for me.
Is there a way to do this with middleware in an easy way (without the Sites framework)?
A little bit of help would be really great. Thanks.</p>

<p>Edit: As I tried it like in the tutorial from above, my httpd.conf looked like this:</p>

<pre><code>ServerRoot "/home/webfactionusername/webapps/erdbeer/apache2"

LoadModule authz_core_module modules/mod_authz_core.so
LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so
LoadModule unixd_module      modules/mod_unixd.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/webfactionusername/logs/user/access_erdbeer.log combined
ErrorLog /home/webfactionusername/logs/user/error_erdbeer.log

Listen 10414
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5

WSGIDaemonProcess erdbeer processes=2 threads=12 python-path=/home/webfactionusername/webapps/erdbeer:/home/webfactionusername/webapps/erdbeer/myproject:/home/webfactionusername/webapps/erdbeer/lib/python2.7
WSGIProcessGroup erdbeer
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /home/webfactionusername/webapps/erdbeer/myproject/myproject/wsgi.py


# Virtual hosts setup
NameVirtualHost *
&lt;VirtualHost *&gt;
    ServerName mydomain123abc.de

    WSGIDaemonProcess erdbeer processes=5 python-path=/home/webfactionusername/webapps/erdbeer:/home/webfactionusername/webapps/erdbeer/lib/python2.7 threads=1
    WSGIScriptAlias / /home/webfactionusername/webapps/erdbeer/subdomain1.wsgi
&lt;/VirtualHost&gt;

&lt;VirtualHost *&gt;
    ServerName seconddomain123.de

    WSGIDaemonProcess erdbeer processes=5 python-path=/home/webfactionusername/webapps/erdbeer:/home/webfactionusername/webapps/erdbeer/lib/python2.7 threads=1
    WSGIScriptAlias / /home/webfactionusername/webapps/erdbeer/subdomain2.wsgi
&lt;/VirtualHost&gt;
</code></pre>

<p>Edit2: I'm still not able to grasp this middleware-concept. I only was able to understand how that I probably need to use "process_request", but I have no clue how the middleware-file would look like. Lets say I have "domain1.com" and "domain2.com" which should use these urls:</p>

<p>domain1_urls.py</p>

<pre><code>from django.conf.urls import include, url
from django.contrib import admin
from django.http import HttpResponse

urlpatterns = [
    url(r'^$', 'myapp1.views.home'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^robots\.txt$', lambda r: HttpResponse("User-agent: *\nDisallow:", content_type="text/plain")),
]
</code></pre>

<p>domains2_urls.py</p>

<pre><code>from django.conf.urls import include, url
from django.contrib import admin
from django.http import HttpResponse

urlpatterns = [
    url(r'^$', 'myapp2.views.home'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^robots\.txt$', lambda r: HttpResponse("User-agent: *\nDisallow:", content_type="text/plain")),
]
</code></pre>

<p>How would I use that in my middleware? (I'm a beginner...)</p>

<p>I found the solution <a href="https://code.djangoproject.com/wiki/MultiHostMiddleware" rel="nofollow">perfectly here</a>.</p>

<pre><code># File: settings.py

HOST_MIDDLEWARE_URLCONF_MAP = {
    # Control Panel
    "www.example.com": "webapp.sites.example.urls",
}
</code></pre>

<p>and</p>

<pre><code># File: multihost.py

import time
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

            # best way to do this.
            host_port = host.split(':')
            if len(host_port)==2:                    
                host = host_port[0] 

            if host in settings.HOST_MIDDLEWARE_URLCONF_MAP:
                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 'MultiHost' in request.META:
            response['MultiHost'] = request.META.get("MultiHost")

        if 'LoadingStart' in request.META:
            _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
</code></pre>

python django django-views