Django to serve multiple sites/countries from a single application
Project description
Django International
A single application to server multiple domains and country sites.
Install
pip install django-international-sites-0.1.0.tar.gz
- Add "international" to
INSTALLED_APPS
insettings.py
- Run
python manage.py migrate international
- Add
CountrySite
objects in admin/admin/international/countrysite/
Settings
# Fallback country code
DEFAULT_COUNTRY_CODE = "NL"
# Map domains uniquely to a single country code (optional)
UNIQUE_DOMAINS = {"projectcece.nl": "nl", "projectcece.co.uk": "uk"}
# Directory for site icons to be displayed in admin (optional)
SITE_ICON_DIR = "static/site_icons/"
Request middleware
How is the country code detected from the request?
- If unique domain name (e.g. projectcece.nl), use country data of related country code
- If country code is forced as url parameter (i.e. projectcece.com/c=fr), use that country code
- If a cookie with location preference is used, use that country code
- Check location based on visitor IP address, use that country code
- If nothing could be detected, use default country code
Add the middleware to settings:
MIDDLEWARE = [
...
'international.middleware.InternationalSiteMiddleware',
'django.middleware.locale.LocaleMiddleware',
'vinaigrette.middleware.VinaigretteAdminLanguageMiddleware'
]
This makes the country_site
available through the request object. E.g., in views:
def index(request):
country_code = request.country_code
Models
All models in a project can be made international, i.e. associated to countries and/or languages, by inheriting the InternationalModel
base class.
# models.py
from international.models import InternationalModel
class Product(InternationalModel):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
This will add the country_sites
and object_language
field and extend the model managers.
To filter all models associated with CountrySite nl
or with language "en":
products = Product.objects.by_country("nl")
products = Product.objects.by_language("en")
Language
When using in combination with Django's i18n translation, add the InternationalSiteMiddleware
before the Django LocaleMiddleware
in your project's settings.
If you want to force using the CountrySite.default_language
language for a given CountrySite, set FORCE_COUNTRY_LANGUAGE
to True. This will make sure that for e.g. the German country site, i18n
will use the German language that has been associated to the CountrySite.
# settings.py
FORCE_COUNTRY_LANGUAGE = True
Admin Mixins
InternationalModelAdminMixin
For models inheriting the InternationalModel class
TranslatedFieldsModelAdminMixin
For models using Vinaigrette translated fields - not InternationalModel
Extend model admin's with fields that use django-vinaigrette to add translations (using gettext
instead of adding more fields in the db). In order to use this mixin the translated fields must be registerred to Vinaigrette in the app's config like below. Specifically, the translated_fields
dictionary must be available in the AppConfig.
class TestappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'testapp'
translated_fields = {
'Certificate': ['description']
}
def ready(self):
for modelname in self.translated_fields.keys():
model = self.get_model(modelname)
# Register fields to translate
vinaigrette.register(model, self.translated_fields[modelname])
This will show the translated field indicator with all fields that can be translated in the translation files. It will also add the current translations to the bottom of the admin page (note: these are for reference and cannot be edited through the admin since they do not come from the database but the translation files)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-international-sites-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | edb8eae257a30567fb383afc9902e269a9aac0794803dbff324a5f4744f82edc |
|
MD5 | 20968b1df134dcc186d77ed9ec103ec5 |
|
BLAKE2b-256 | 9452769207e1d733a7821c7cfc469ce22b2026959c65a7c880301b371ed2b4a3 |
Hashes for django_international_sites-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58eaab36a2c07f8bc5de7b17c68c267707375e1f23245d5ad41b7fbc9fe818a8 |
|
MD5 | bcafc76e1330c35cafe2548933604ef7 |
|
BLAKE2b-256 | 84559fed8124d42b9866be1e96c1e8cf9ad2861d0228eb38e0d576ac7d7b37aa |