Skip to main content

django-db-chat-widget

Django integration for db-chat-widget: drop a natural-language database chatbot into any Django template as a floating popup (or inline panel), backed by your existing Django database connection.

  • Reuses your existing Django database connection — no separate credentials to configure; the SQLAlchemy URL is derived automatically from settings.DATABASES (PostgreSQL, MySQL, SQLite).
  • Pluggable LLM backend: Anthropic Claude, OpenAI, Groq, or a local Ollama model (via db-chat-widget).
  • Read-only by default: generated SQL is parsed and only SELECT statements are allowed unless you explicitly opt into write access.
  • One template tag: {% db_chat_widget %} renders a floating chat bubble (or an inline panel) — no extra views or URLs to wire up yourself.
  • Proper CSRF handling: works with Django's CSRF protection out of the box, no csrf_exempt required.
  • Holds a real conversation: remembers prior turns per visitor, in an in-memory store that's never written to your own database — not just database questions, ordinary chit-chat too — and always replies in whatever language the visitor is writing in.

Install

pip install django-db-chat-widget

This pulls in db-chat-widget (the framework-agnostic core: LLM providers, SQL safety checks, query execution) as a dependency.

Add the app and its URLs:

# settings.py
INSTALLED_APPS = [
    ...,
    "django.contrib.staticfiles",
    "django_db_chat_widget",
]

TEMPLATES = [
    {
        ...,
        "OPTIONS": {
            "context_processors": [
                ...,
                "django.template.context_processors.request",  # required
            ],
        },
    },
]

DB_CHAT_WIDGET = {
    "DB_ALIAS": "default",           # which DATABASES entry to query
    "LLM_PROVIDER": "groq",          # "anthropic" | "openai" | "groq" | "ollama"
    "LLM_API_KEY": "gsk_...",        # or read from os.environ
    "READ_ONLY": True,               # default: only SELECT statements allowed
    "MAX_ROWS": 200,
    "ALLOWED_TABLES": None,          # e.g. ["orders", "customers"] to scope access
    "TITLE": "Ask your database",
    "HISTORY_MAX_TURNS": 10,         # previous turns remembered per visitor; 0 disables memory
}
# urls.py
from django.urls import include, path

urlpatterns = [
    ...,
    path("db-chat/", include("django_db_chat_widget.urls")),
]

Then drop the tag into any template:

{% load db_chat_widget %}
{% db_chat_widget %}

That's it — a floating chat bubble now appears in the bottom-right corner of that page. See example_project/ for a complete, runnable Django project demonstrating this end to end:

cd example_project
python seed_db.py          # seeds sample customers/orders tables
python manage.py runserver

Template tag options

{% db_chat_widget %}                                {# popup, bottom-right #}
{% db_chat_widget position="bottom-left" %}          {# popup, bottom-left #}
{% db_chat_widget target="#my-chat-div" %}           {# inline instead of popup #}
{% db_chat_widget title="Ask HR" placeholder="..." %}

Multiple databases / explicit connection

If your Django project has several DATABASES entries, point DB_CHAT_WIDGET["DB_ALIAS"] at the one to query. To bypass Django's DATABASES entirely (e.g. a read replica not declared there), set DB_CHAT_WIDGET["DB_URL"] to an explicit SQLAlchemy URL instead — it takes precedence over DB_ALIAS.

Safety notes

  • READ_ONLY=True (the default) is enforced by parsing the generated SQL with sqlglot, not just by prompting the model — treat it as the actual security boundary. See db-chat-widget's safety notes for details.
  • Use ALLOWED_TABLES to scope the chatbot away from sensitive tables (e.g. Django's own auth_user / session tables).
  • Conversation history is kept in an in-memory store, isolated per visitor by an opaque cookie — it is never written to your own database, and never shared across visitors. It doesn't survive a server restart and isn't shared across multiple worker processes; swap in your own shared store (e.g. Redis) ahead of ChatEngine.ask for multi-worker deployments.
  • The /db-chat/chat/ endpoint is a normal Django view protected by Django's CSRF middleware; the template tag ensures the CSRF cookie is set on any page that renders it.

Development

pip install -e ".[dev]"
pytest
ruff check .

License

MIT

Metadata

Release files for django-db-chat-widget 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-db-chat-widget 0.2.1
File Size Uploaded Provenance Actions
django_db_chat_widget-0.2.1.tar.gz 15.9 kB Without provenance Details for django_db_chat_widget-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-db-chat-widget 0.2.1
File Interpreter ABI Platform Actions
django_db_chat_widget-0.2.1-py3-none-any.whl Python 3 none any Details for django_db_chat_widget-0.2.1-py3-none-any.whl

Total release size: 29.9 kB

Release files / django_db_chat_widget-0.2.1.tar.gz

Download URL django_db_chat_widget-0.2.1.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
901454b18abc7aa4ae1192affc4b3dbec5e78019beeea6ab10e59fb55acba26e
BLAKE2b-256 checksum
How to use checksums
e617adbc11ce9461246b15099a906a1205fb7befaaa8377c4523ca6b4c6d6f37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / django_db_chat_widget-0.2.1-py3-none-any.whl

Download URL django_db_chat_widget-0.2.1-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a09a3e544fc6bbc67ae295470fa4554c42d130734a972816157e2f01123b6b35
BLAKE2b-256 checksum
How to use checksums
6718e019987326e653e3c556d145aadd81485d8635f21383b63050bf697408af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for django-db-chat-widget 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page