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 via request.session — 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.sessions",      # required — conversation memory lives here
    "django.contrib.staticfiles",
    "django_db_chat_widget",
]

MIDDLEWARE = [
    ...,
    "django.contrib.sessions.middleware.SessionMiddleware",  # required
    ...,
]

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
}

django.contrib.sessions and its middleware are already present in almost every Django project by default (part of startproject); if yours has them, there's nothing extra to do.

# 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 manage.py migrate   # creates Django's own tables (sessions, etc.)
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 stored per visitor in request.session — isolated the same way any other session data is (per browser session, whatever SESSION_ENGINE your project already uses). It is never shared across visitors.
  • 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.0

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.0
File Size Uploaded Provenance Actions
django_db_chat_widget-0.2.0.tar.gz 15.9 kB Without provenance Details for django_db_chat_widget-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 29.8 kB

Release files / django_db_chat_widget-0.2.0.tar.gz

Download URL django_db_chat_widget-0.2.0.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f091add9e3523fecb9dac09221be889ef66cadf90efd6b86f8b692e6eea082ce
BLAKE2b-256 checksum
How to use checksums
727d5e22530a8d1801f5be6113bd65bb25613892bd85f775f39fe85fbf2d4358
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.0-py3-none-any.whl

Download URL django_db_chat_widget-0.2.0-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc82e06045698974fe13cae4e11d81400d3b707e61e3dfbf28fcdd2e35b137e2
BLAKE2b-256 checksum
How to use checksums
5349f2f7df89ab44a358f8985dbdfae6b48340650201856e78fcbca38b5bb16d
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.0

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