Package: django-usersettings2
A reusable app for django, provides the ability to configure site settings via the admin interface, rather than by editing settings.pyWhy would you use usersettings?This project is the missing extension to the Django “sites” framework, use it to store additional information for your Django-powered sites. The project structure is heavily inspired by  djangositesapp, with aone-to-onerelationship to theSitemodel.It’s best explained through examples.Example UsageFor example, suppose you’re developing a multi-site django project i.e. using single Django installation that powers more than one site and you need to differentiate between those sites in some way.(e.g. Site Title, Physical Location, Contact Details… etc)Of course, you could hardcode the information in the templates and use different templates
for each site. Alternatively you could configure details in yoursettings.pyfor each site.A better solution would be to usedjango-usersettings2. This project accomplishes several things quite nicely:It lets the site producers edit all settings – for multiple sites – in a single interface (the Django admin).It lets the site developers use the same Django views/templates for multiple sites.To get started, create a class that inherits fromusersettings.models.UserSettings. Make sure to import theUserSettingsmodel. Your class should live in one of your apps’ models.py (or module).SinceUserSettingsmodel inherit fromdjango.db.models.Model, you are free to add any field you want.Here’s a simple example:fromdjango.dbimportmodelsfromdjango.utils.translationimportugettext_lazyas_fromusersettings.modelsimportUserSettingsclassSiteSettings(UserSettings):site_title=models.CharField(_('Site Title'),max_length=100)tag_line=models.CharField(_('Tag Line'),max_length=150,blank=True)site_description=models.TextField(_('Site Description'),blank=True)...classMeta:verbose_name='Site Settings'verbose_name_plural='Site Settings'If you followed the Django tutorial, this shouldn’t look too new to you.
The only difference to normal models is that you subclassusersettings.models.UserSettingsrather thandjango.db.models.base.Model.Hooking the ‘usersettings’ to the admin siteTo make your new model editable in the admin interface, you must first create an admin class that subclassesusersettings.admin.SettingsAdmin. Continuing with the example model above, here’s a simple correspondingSiteSettingsAdminclass:fromdjango.contribimportadminfromdjango.utils.translationimportugettext_lazyas_fromusersettings.adminimportSettingsAdminfrom.modelsimportSiteSettingsclassSiteSettingsAdmin(SettingsAdmin):fieldsets=((_('Site Title / Tag Line'),{'description':'...','fields':('site_title','tag_line',)}),...)...admin.site.register(SiteSettings,SiteSettingsAdmin)SinceSettingsAdmininherits from ModelAdmin, you’ll be able to use the normal
set of Django ModelAdmin properties, as appropriate to your circumstance.Once you’ve registered your admin class, a new model will appear in the top-level admin list.Hooking into the current usersettings from viewsYou can use theusersettingsin your Django views to do particular things based on theusersettingsfor the site.Here’s an example of what the a view looks like:fromusersettings.shortcutsimportget_current_usersettingsdefhome(request):...current_usersetting=get_current_usersettings()context={'title':current_usersetting.site_title,}...Custom MiddlewareTo avoid the repetitions of having to importcurrent_usersettingfor every view. Addusersettings.middleware.CurrentUserSettingsMiddlewaretoMIDDLEWARE_CLASSESThe middleware sets theusersettingsattribute on every request object, so you can userequest.usersettingsto get the current usersettings:MIDDLEWARE_CLASSES=(...'usersettings.middleware.CurrentUserSettingsMiddleware',...)Caching the currentUserSettingsobjectAs theusersettingsare stored in the database, each call toUserSettings.objects.get_current()could result in a database query.But just like the Django sites framework, on the first request the current usersettings is cached, and any subsequent call returns the cached data instead of hitting the database.If for any reason you want to force a database query, you can tell Django to clear the cache usingUserSettings.objects.clear_cache():fromusersettings.shortcutsimportget_usersettings_modelUserSettings=get_usersettings_model()# First call; current usersettings fetched from database.current_usersetting=UserSettings.objects.get_current()# Second call; current usersettings fetched from cache.current_usersetting=UserSettings.objects.get_current()# Force a database query for the third call.UserSettings.objects.clear_cache()current_usersetting=UserSettings.objects.get_current()InstallInstalldjango-usersettings:pipinstalldjango-usersettings2AddusersettingstoINSTALLED_APPS:INSTALLED_APPS=(...'usersettings',...)Specify the customUserSettingsmodel as the default usersettings model for your project using theUSERSETTINGS_MODELsetting in your settings.py (required):USERSETTINGS_MODEL='config.SiteSettings'Addusersettings.middleware.CurrentUserSettingsMiddlewaretoMIDDLEWARE_CLASSES(optional).The middleware sets theusersettingsattribute on every request object, so you can userequest.usersettingsto get the current usersettings:MIDDLEWARE_CLASSES=(...'usersettings.middleware.CurrentUserSettingsMiddleware',...)The current usersettings are made available in the template context when yourTEMPLATE_CONTEXT_PROCESSORSsetting containsusersettings.context_processors.usersettings:TEMPLATE_CONTEXT_PROCESSORS=(...'usersettings.context_processors.usersettings',...)Dependenciesdjango-usersettings2 requires Thesitesframework to be installed.To enable the sites framework, follow these steps:Adddjango.contrib.sitesto yourINSTALLED_APPSsetting:INSTALLED_APPS=(...'django.contrib.sites'...)Define aSITE_IDsetting:SITE_ID=1Run migrate.DJANGO-CMS >= 3.0 Toolbardjangocms-usersettings2integratesdjango-usersettings2withdjango-cms>=3.0This allows a site editor to add/modify all usersettings in the frontend editing mode of django CMS and provide your users with a streamlined editing experience.DocumentationThe full documentation is athttps://django-usersettings2.readthedocs.org.History0.1.0 (2014-09-05)First release on PyPI.