Skip to main content

Django utilities for injecting flash messages into requests and responses

Project description

django-inject-message

Inject HTML messages into Django HTTP responses based on URL patterns, anchor sequences, and activity rules.

Purpose

Manage banners, alerts, and notices from the database (or admin) without editing templates. Messages are inserted into rendered HTML responses by middleware after matching:

  • URL path patterns
  • Anchor sequences in the HTML body (e.g. <body>, </header>)
  • Activity window (active_from / active_until)
  • Optional cron schedule (weekends, business hours, etc.)
  • Severity level with built-in CSS styling

Installation

pip install django-inject-message

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "django_boosted",
    "inject_message",
]

Add middleware (before compression middleware if you use GZip):

MIDDLEWARE = [
    # ...
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django_boosted.middleware.CurrentUserMiddleware",
    "inject_message.middleware.InjectMessageMiddleware",
]

Run migrations:

python manage.py migrate inject_message

Quick start

1. Create a message and injection rule

from inject_message.models import Message, MessageInjection, MessageLevel

message = Message.objects.create(
    label="Maintenance",
    content="<strong>Scheduled maintenance tonight at 22:00.</strong>",
    level=MessageLevel.WARNING,
)

MessageInjection.objects.create(
    message=message,
    url_pattern="/dashboard/",
    after_sequence="<body>",
)

2. Or use the Django admin

Open /admin/ and manage Messages with inline Message injections. Each message supports:

  • Content and severity level (debug, info, success, warning, error)
  • Activity dates and cron schedule (with presets for weekends / weekdays)
  • Multiple injection rules per message

3. Schedule examples

from inject_message import SCHEDULE_WEEKENDS, SCHEDULE_WEEKDAYS, SCHEDULE_BUSINESS_HOURS

Message.objects.create(
    label="Weekend promo",
    content="Special offer this weekend!",
    level=MessageLevel.SUCCESS,
    schedule=SCHEDULE_WEEKENDS,
)

Cron format: minute hour day month weekday (e.g. * * * * 0,6 for weekends).

Demo (development)

This repository includes a demo app under tests/demo/:

URL Scenario
/demo/single/ One message
/demo/multiple/ Several messages
/demo/multiple-places/ One message, multiple anchors

Seed demo data:

PYTHONPATH=src python manage.py seed_demo_messages --clear
PYTHONPATH=src python manage.py runserver

Security

Message content is injected as HTML. Only trusted staff should edit messages in the admin.

Development

PYTHONPATH=src python -m pytest tests/ -v
PYTHONPATH=src python manage.py migrate

License

MIT

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_inject_message-0.1.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_inject_message-0.1.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file django_inject_message-0.1.0.tar.gz.

File metadata

  • Download URL: django_inject_message-0.1.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for django_inject_message-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c6e1cfb221f25f60cf9134e0ff27dba8c6a27bf1cea6af5fa3bc1325ffb806d5
MD5 09a5a806d32bdb871d92ebffa45fffb7
BLAKE2b-256 e193e32ab24b666c9714d8ca2fa6b61b2450d454eac9638ffb9df43545c0ff77

See more details on using hashes here.

File details

Details for the file django_inject_message-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_inject_message-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3df6db5f091d6f032b94b5d1c2eeb0e21bae0787c02418740f4a24e563e07180
MD5 ce1b5e4f21d68a9d837d7b6ce6ded5b2
BLAKE2b-256 d0f2054d35d000c0f7ae44574de3442314104edd7e9e31c36248eab24a3c07b2

See more details on using hashes here.

Supported by

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