Skip to main content

A Django app for managing site-wide variables.

Project description

Django SiteVars

A Django app for managing site-wide variables. Ever have a need to store some small value related to a site? An analytics ID perhaps, or a copyright statement. SiteVars provides a simple and efficient way to store those values in your database and edit them through the Django admin interface.

Installation

To install the package, use pip:

pip install django-sitevars

Add sitevars to your INSTALLED_APPS in your Django settings. Optionally, you can configure the provided context processor to add your site variables into every template context.

INSTALLED_APPS = [
    ...
    'django.contrib.sites',  # required
    'sitevars',  # Must come after contrib.sites for admin to work
    ...
]
TEMPLATES=[
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.request",  # required
                "sitevars.context_processors.inject_sitevars",  # optional
            ]
        },
    }
]

Usage

In templates, load the sitevars library to use the included template tag.

{% load sitevars %} Hello, {% sitevar "name" default="world" %}!

Or, if you are using the inject_sitevars context processor, the variable will already be in the template context.

{% load sitevars %} Hello, {{ name|default:"world" }}!

In your views, you can access site variables via the accessor on the site object. Use the get_value method to retrieve the value by name.

from django.contrib.sites.shortcuts import get_current_site

def my_view(request):
  site = get_current_site(request)
  name = site.vars.get_value("name", default="world")
  ...

To reduce load on the database, sitevars maintains a cache of all variables per site (using the default cache configured in your Django project). If you prefer not to use the cache for some reason, you can disable it in your settings file.

SITEVARS_USE_CACHE = False

Development

I recommend using Astral's uv to manage your local development environment. This project uses pre-commit. After installing uv, clone this repository, then:

uv venv
uv pip install -e .[dev]
pre-commit install

Tests are run using pytest and tox.

pytest test_project  # unit tests
tox run  # full test matrix

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

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

django_sitevars-1.0.2.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

django_sitevars-1.0.2-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file django_sitevars-1.0.2.tar.gz.

File metadata

  • Download URL: django_sitevars-1.0.2.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for django_sitevars-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c92c3ed2fa45f4ade96e3c70d1130368883cd63e36689de60b99fab1d88055bc
MD5 4bc9d896260bb1e9c172227dd289253a
BLAKE2b-256 d05e4fcf9c75be9a1e372b210de2483d1d67b37492f6574647cefd203d50b79d

See more details on using hashes here.

File details

Details for the file django_sitevars-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sitevars-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 71ac33cbeec76a29a30a207b5c3e7b1476067acfcc2f2f0d7b03ac62a768c817
MD5 85ab99f6baaa743a8f806a201b8c7019
BLAKE2b-256 c736d7b0f979d7f1196122ef668abb27668d4dc7916b6e4ce07422157107d654

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page