1. Introduction
Alternative implementation of django "sites" framework based on settings instead of models.
The main motivation for make this package is solve in a simple way to make urls with correspondig domain in each environment (production, development, etc…)
Also, I consider that use database for store a site configuration, is slighty overkill.
This package do same or similar thing that original "sites" framework of django, but does not intends to solve the same use cases.
2. Requirements
-
Python 2.7, 3.3, 3.4, 3.5 or 3.6
-
Django 1.8, 1.9 and 2.0
3. User guide
3.1. Installation
The simplest way to install django-sites is using pip:
pip install django-sites
3.2. Configure
Add add django_sites
to django installed apps list:
INSTALLED_APPS += ('django_sites',)
And configure your default sites:
# settings.py
SITES = {
"dev": {"domain": "localhost:8000", "scheme": "http", "name": "localhost"},
"pro": {"domain": "somehost.com", "scheme": "https", "name": "somehost"},
}
# settings_development.py
# defaut site id for development environment
SITE_ID = "dev"
# settings_production.py
# defaut site id for production environment
SITE_ID = "pro"
3.3. Python Api reference
3.3.1. django_sites.get_current()
Returns a current site instance depending on settings.SITE_ID
value. It
can raise django_sites.exceptions.SitesNotConfigured
.
3.3.2. django_sites.get_by_id(id)
Returns a site instance corresponding to specified id. It can raise
django_sites.exceptions.SiteNotFound
exception in case of
key not matches any configured site.
>>> from django_sites import get_by_id as get_site_by_id
>>> site = get_site_by_id("pro")
>>> site.domain
'somehost.com'
>>> site.scheme
'https'
>>> site.name
'somehost'
3.3.3. django_sites.reverse(*args, **kwargs)
This function works like django’s reverse function but appends the current site scheme, and domain to the url.
>>> from django_sites import reverse as sites_reverse
>>> sites_reverse('ns:foo')
'http://example.com/foo'
3.4. Jinja2 Api Reference
If you are also django-jinja
user, django-sites
exposes two helper functions
for use it from jinja2 templates.
3.4.1. Reverse urls in templates
<a href="{{ sites_url('foo') }}">Link</a>
<a href="{{ sites_url('foo', site_id='pro') }}">Link</a>
<a href="http://localhost:8000/foo">Link</a>
<a href="https://somehost.com/foo">Link</a>
3.4.2. Reverse static urls
<script src="{{ sites_static('lib.js') }}"></script>
<script src="{{ sites_static('lib.js', site_id='pro') }}"></script>
<script src="http://localhost:8000/static/lib.js"></script>
<script src="https://somehost.com/static/lib.js"></script>
4. License
Copyright (c) 2013-2016 Andrey Antukh <niwi@niwi.nz>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.