The missing extension to the Django “sites” framework, use it to store additional information for your Django-powered sites.
Project description
A reusable app for django, provides the ability to configure site settings via the admin interface, rather than by editing settings.py
Why 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 django sites app, with a one-to-one relationship to the Site model.
It’s best explained through examples.
Example Usage
For 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 your settings.py for each site.
A better solution would be to use django-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 from usersettings.models.UserSettings. Make sure to import the UserSettings model. Your class should live in one of your apps’ models.py (or module).
Since UserSettings model inherit from django.db.models.Model, you are free to add any field you want.
Here’s a simple example:
from django.db import models
from django.utils.translation import ugettext_lazy as _
from usersettings.models import UserSettings
class SiteSettings(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)
...
class Meta:
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 subclass usersettings.models.UserSettings rather than django.db.models.base.Model.
Hooking the ‘usersettings’ to the admin site
To make your new model editable in the admin interface, you must first create an admin class that subclasses usersettings.admin.SettingsAdmin. Continuing with the example model above, here’s a simple corresponding SiteSettingsAdmin class:
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from usersettings.admin import SettingsAdmin
from .models import SiteSettings
class SiteSettingsAdmin(SettingsAdmin):
fieldsets = (
(_('Site Title / Tag Line'), {
'description': '...',
'fields': ('site_title', 'tag_line',)
}),
...
)
...
admin.site.register(SiteSettings, SiteSettingsAdmin)
Since SettingsAdmin inherits 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 views
You can use the usersettings in your Django views to do particular things based on the usersettings for the site.
Here’s an example of what the a view looks like:
from usersettings.shortcuts import get_current_usersettings
def home(request):
...
current_usersetting = get_current_usersettings()
context = {
'title': current_usersetting.site_title,
}
...
Custom Middleware
To avoid the repetitions of having to import current_usersetting for every view. Add usersettings.middleware.CurrentUserSettingsMiddleware to MIDDLEWARE_CLASSES The middleware sets the usersettings attribute on every request object, so you can use request.usersettings to get the current usersettings:
MIDDLEWARE_CLASSES = (
...
'usersettings.middleware.CurrentUserSettingsMiddleware',
...
)
Caching the current UserSettings object
As the usersettings are stored in the database, each call to UserSettings.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 using UserSettings.objects.clear_cache():
from usersettings.shortcuts import get_usersettings_model
UserSettings = 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()
Install
Install django-usersettings:
pip install django-usersettings2
Add usersettings to INSTALLED_APPS:
INSTALLED_APPS = ( ... 'usersettings', ... )
Specify the custom UserSettings model as the default usersettings model for your project using the USERSETTINGS_MODEL setting in your settings.py (required):
USERSETTINGS_MODEL = 'config.SiteSettings'
Add usersettings.middleware.CurrentUserSettingsMiddleware to MIDDLEWARE_CLASSES (optional).
The middleware sets the usersettings attribute on every request object, so you can use request.usersettings to get the current usersettings:
MIDDLEWARE_CLASSES = ( ... 'usersettings.middleware.CurrentUserSettingsMiddleware', ... )
The current usersettings are made available in the template context when your TEMPLATE_CONTEXT_PROCESSORS setting contains usersettings.context_processors.usersettings:
TEMPLATE_CONTEXT_PROCESSORS = ( ... 'usersettings.context_processors.usersettings', ... )
Dependencies
django-usersettings2 requires The sites framework to be installed.
To enable the sites framework, follow these steps:
Add django.contrib.sites to your INSTALLED_APPS setting:
INSTALLED_APPS = ( ... 'django.contrib.sites' ... )
Define a SITE_ID setting:
SITE_ID = 1
Run migrate.
DJANGO-CMS >= 3.0 Toolbar
djangocms-usersettings2 integrates django-usersettings2 with django-cms>=3.0
This 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.
Documentation
The full documentation is at https://django-usersettings2.readthedocs.org.
History
0.1.0 (2014-09-05)
First release on PyPI.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-usersettings2-0.1.6.tar.gz
.
File metadata
- Download URL: django-usersettings2-0.1.6.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d4fca0c0375e069294f9352de154cba1d4479835da16a191793ab3a0723ce81 |
|
MD5 | 5971372ad68577569ca552040daac88c |
|
BLAKE2b-256 | 897f5e5ee608c812a9fa4dab0cc982d36b8687bc00e90d53d7c04b6153b4b136 |