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

Uploaded Source

Built Distribution

django_sitevars-1.0.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_sitevars-1.0.1.tar.gz
  • Upload date:
  • Size: 18.5 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.1.tar.gz
Algorithm Hash digest
SHA256 f3236581cfa2571e862dde3e204791207698838720b4f740adcf2395bbaf33c4
MD5 e2c9faaa9625c6a7b4d2e1f72e2b1896
BLAKE2b-256 f09ced49cecaf28befb7182810393ccd1bd942149f3b23cd3755eb973f8f7fb2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_sitevars-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c6b51461e5e8b3f07caddf7643e712bdb8789ba628de420e74e0b373eb2d41c
MD5 2733aaa7eb3142ee586e840098b43270
BLAKE2b-256 60b25beac7d1c6ba70952fc7249300521526ef03044383225ce1d598d7876053

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