A lightweight and extensible chatbot web application built with Django and OpenAI API.
This project demonstrates how to integrate a conversational AI model into a Django-based web interface.
- 💬 Real-time chat interface powered by Django views and AJAX (or Fetch API)
- 🔑 Secure integration with OpenAI API
- 🗂️ Modular app structure for easy extension
- ⚙️ Environment variable support for API keys
- 🧪 Ready for local development or cloud deployment
- Backend: Django 5.x / Python 3.10+
- Frontend: HTML5, CSS3, JavaScript (Fetch API)
- AI Engine: OpenAI API (ChatGPT-style model)
- Environment:
.envfile viapython-dotenvor Django settings
git clone https://github.com/X-astro/simple-chatbot-django.git
cd simple-chatbot-djangopython -m venv venv
source venv/bin/activate # On Windows use venv\Scripts\activatepip install -r requirements.txtCreate a .env file in the project root:
OPENAI_API_KEY=your_api_key_hereOr add it directly to your Django settings (not recommended for production).
python manage.py runserverThen open your browser at:
http://127.0.0.1:8000/
- Open the main chat page.
- Type your question or message.
- The chatbot responds using the configured OpenAI model.
- You can customize prompts and logic inside
chatbot/views.py.
| Variable | Description | Example |
|---|---|---|
OPENAI_API_KEY |
Your OpenAI API key | sk-abc123... |
DEBUG |
Django debug mode | True |
ALLOWED_HOSTS |
Hosts allowed to serve the app | ["localhost", "127.0.0.1"] |
import openai
from django.conf import settings
openai.api_key = settings.OPENAI_API_KEY
def ask_openai(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
)
return response['choices'][0]['message']['content']- You can extend the chat logic for memory, database logging, or multi-user support.
- Integrate WebSocket (Django Channels) for real-time responses.
- Style the frontend using Bootstrap, Tailwind, or your preferred framework.
This project is licensed under the MIT License — free for personal and commercial use.
Contributions are welcome!
- Fork the repo
- Create your feature branch
- Submit a pull request
Astro X
“Learning by doing — one project at a time.”
Enjoy building your AI-powered Django chatbot! 🚀
