1

我有 django 项目,其中移动部分 settings.py 配置如下

MIDDLEWARE_CLASSES = (
                      'site_middleware.SetSiteFromHost',
                     )
SITE_ID = 1

和 site_middleware.py :

class SetSiteFromHost:


    def process_request(self, request):
        if mobileBrowser(request):
            if request.GET.get('server', False):

                request.session['server_mode'] = True
                site = 1
            elif request.session.get('server_mode') == True:

                site = 1
            else:

                request.session['server_mode'] = False
                site = 2

        else:
            site = 1
        settings.SITE_ID = site

错误未显示,但切换不起作用?

4

1 回答 1

2

来自文档:在运行时更改设置

您不应在运行时更改应用程序中的设置。

from django.conf import settings

settings.DEBUG = True   # Don't do this!

您应该分配给设置的唯一位置是在设置文件中。

我认为最好的方法是制作几个设置文件:

文件base_settings.py

 ... all your settings for all sites ...

文件first_site_settings.py

from base_settings import *

SITE_ID = 1

... other settings for this site ...

文件second_site_settings.py

from base_settings import *

SITE_ID = 2

... other settings for this site ...

并将每个站点作为单独的 djano 进程运行:

$ python mysite/manage.py runserver --settings=mysite.first_site_settings
$ python mysite/manage.py runserver --settings=mysite.second_site_settings
于 2012-10-11T12:23:51.623 回答