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.0.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

django_sitevars-1.0.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_sitevars-1.0.0.tar.gz
  • Upload date:
  • Size: 18.2 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.0.tar.gz
Algorithm Hash digest
SHA256 1e20871e839f679daf58362a6eddc248885cbef72519827faafd6850f4dc7cad
MD5 d98f62dcfabf7254a104dff2c2c0c0f0
BLAKE2b-256 8cf6304df1b58e749882092ea733a9423f06d874f5723d87f333647f8f12e047

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_sitevars-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b575a85463a097832d5403800fb28ef155febce7c9ee440a181920a429b58a4e
MD5 3b715b5afb422f8813434be0da319b1a
BLAKE2b-256 0dcdddbdc8f10587014dd1ae44abc0a96dc3aec895a1a8ae7a9ac5968967d262

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