多个域 - 一个django项目

时间:2016-10-14 14:36:20

标签: python django django-views

我想在多个域/网站上运行一个django项目。每个网站都需要访问一个独特的" urls.py" /" views.py"。我已经用this tutorial尝试了它,但它对我不起作用。 有没有办法以简单的方式使用中间件(没有Sites框架)? 一点点帮助真的很棒。感谢。

编辑:正如我在上面的教程中尝试的那样,我的httpd.conf看起来像这样:

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\" %>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 *
<VirtualHost *>
    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
</VirtualHost>

<VirtualHost *>
    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
</VirtualHost>

Edit2:我仍然无法掌握这个中间件概念。我只能理解我可能需要使用&#34; process_request&#34;,但我不知道中间件文件是怎么样的。可以说我有&#34; domain1.com&#34;和&#34; domain2.com&#34;应该使用这些网址:

domain1_urls.py

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")),
]

domains2_urls.py

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")),
]

我如何在我的中间件中使用它? (我是初学者......)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案perfectly here

# File: settings.py

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

# 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