从单个django应用程序提供多个站点
django-multisite的Python项目详细描述
自述文件
使用pip安装:
pip install django-multisite
或者通过git获取代码:
git clone git://github.com/ecometrica/django-multisite.git django-multisite
然后运行:
python setup.py install
或者将django multisite/multisite文件夹添加到pythonpath。
如果您想参与,请运行:
python setup.py develop
快速启动
将settings.py中的站点id替换为:
from multisite import SiteID SITE_ID = SiteID(default=1)
将这些添加到已安装的应用程序:
INSTALLED_APPS = [ ... 'django.contrib.sites', 'multisite', ... ]
将加载程序添加到“选项”部分中的settings.py templates中:
TEMPLATES = [ ... { ... 'DIRS': {...} 'OPTIONS': { 'loaders': ( 'multisite.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ) } ... } ... ]
编辑settings.py中间件(django的中间件类<;1.10):
MIDDLEWARE = ( ... 'multisite.middleware.DynamicSiteMiddleware', ... )
附加到settings.py,以便使用可以 安全清除:
# The cache connection to use for django-multisite. # Default: 'default' CACHE_MULTISITE_ALIAS = 'multisite' # The cache key prefix that django-multisite should use. # If not set, defaults to the KEY_PREFIX used in the defined # CACHE_MULTISITE_ALIAS or the default cache (empty string if not set) CACHE_MULTISITE_KEY_PREFIX = ''
如果已将缓存多站点别名设置为自定义值,,例如'multisite',将单独的后端添加到settings.py缓存:
CACHES = { 'default': { ... }, 'multisite': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'TIMEOUT': 60 * 60 * 24, # 24 hours ... }, }
多站点通过检查所有别名域来确定允许的主机。你可以 还可以将多站点附加主机设置为包括附加主机。这个罐头 包括通配符。:
MULTISITE_EXTRA_HOSTS = ['example.com'] # will match the single additional host MULTISITE_EXTRA_HOSTS = ['.example.com'] # will match any host ending '.example.com'
开发环境
当处于“开发模式”(默认为 与默认站点ID关联的别名。
- 开发模式为:
- 运行测试,即manage.py测试
- 在settings.debug=true中本地运行,其中主机名是顶级名称,即localhost
要在本地环境中使用多站点别名,请将条目添加到 本地etc/hosts文件以匹配应用程序中的别名。例如
127.0.0.1 example.com 127.0.0.1 examplealias.com
并访问example.com:8000或exampleAlias.com:8000而不是 通常的本地主机:8000。
域回退
默认情况下,如果域名未知,multisite将用 HTTP 404未找到错误。若要更改此行为,请添加 设置.py:
# The view function or class-based view that django-multisite will # use when it cannot match the hostname with a Site. This can be # the name of the function or the function itself. # Default: None MULTISITE_FALLBACK = 'django.views.generic.base.RedirectView # Keyword arguments for the MULTISITE_FALLBACK view. # Default: {} MULTISITE_FALLBACK_KWARGS = {'url': 'http://example.com/', 'permanent': False}
模板
如果需要,为域级模板创建模板子目录(在 在settings.templates['dirs']中指定的位置。
多站点的模板加载器将在名为的文件夹中查找模板 域,例如:
templates/example.com
模板加载器还将在 可选的多站点默认模板目录设置,例如:
templates/multisite_templates
测试
运行测试:
python setup.py test
或:
pytest
在部署更改之前,要通过运行来验证它没有破坏任何内容:
tox
这将在django和python的每个支持组合下运行测试。