Skip to main content

Django integration for db-chat-widget: a natural-language database chatbot widget for Django templates.

Project description

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.

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",
}
# 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.

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).
  • 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

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

Uploaded Source

Built Distribution

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

django_db_chat_widget-0.1.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for django_db_chat_widget-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cb57ba8fca8a10265ea97d4051dba4d63d7f09301b991f87b7bdea79747ab431
MD5 e39cb6b45d751adbd4b4b99ab17b8cb6
BLAKE2b-256 762c10be58f20f372d849394360ecc9aba797c397df6583634ffd675b187f94b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_db_chat_widget-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 223d236fbef7c2c003e0b826577eb555660002ff1fc055553d64dff9f70ccc3b
MD5 81d46bc9f087517d0202e141a9a1c319
BLAKE2b-256 51d95e467a34b74e04b0f27ae9fc38a261b28810e038d7a59b8980f047e975c7

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