📝 Overview
This tutorial walks you through how to integrate Ollama—a powerful local LLM runtime—with a Django application. You’ll learn how to:
- Set up a Django project.
- Integrate Ollama to serve local AI responses.
- Create a simple frontend to interact with the model.
- Deploy the app to a server (e.g., via Docker + Render or GCP).
🧰 Prerequisites
- Python 3.9+
- pip
- Git
- Docker (for deployment)
- Ollama installed locally
📦 Step 1: Create Your Django Project
mkdir ollama_django_chatbot && cd ollama_django_chatbot
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install django
django-admin startproject core .
python manage.py startapp chatbot
🧠 Step 2: Install and Run Ollama
- Download and install Ollama
- Pull a model (e.g., Mistral):
ollama pull mistral
- Run the model:
ollama run mistral
This exposes an API locally at
http://localhost:11434
🛠️ Step 3: Connect Django to Ollama
Install requests
to send HTTP queries
pip install requests
Update chatbot/views.py
import requests
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json
OLLAMA_API_URL = 'http://localhost:11434/api/generate'
@csrf_exempt
def ollama_chat(request):
if request.method == 'POST':
body = json.loads(request.body)
prompt = body.get('prompt', '')
payload = {
"model": "mistral",
"prompt": prompt,
"stream": False
}
response = requests.post(OLLAMA_API_URL, json=payload)
data = response.json()
return JsonResponse({'response': data.get('response', '')})
return JsonResponse({'error': 'POST method required'}, status=400)
🌐 Step 4: Create a Simple Frontend
Update core/urls.py
from django.contrib import admin
from django.urls import path
from chatbot.views import ollama_chat
from django.views.generic import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='chat.html'), name='home'),
path('api/chat/', ollama_chat, name='ollama_chat'),
]
Create chatbot/templates/chat.html
<!DOCTYPE html>
<html>
<head>
<title>Ollama Chatbot</title>
</head>
<body>
<h1>Talk to Ollama</h1>
<textarea id="prompt" rows="4" cols="50"></textarea><br>
<button onclick="sendPrompt()">Send</button>
<pre id="response"></pre>
<script>
async function sendPrompt() {
const prompt = document.getElementById("prompt").value;
const res = await fetch("/api/chat/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ prompt })
});
const data = await res.json();
document.getElementById("response").textContent = data.response;
}
</script>
</body>
</html>
🚀 Step 5: Run Locally
python manage.py migrate
python manage.py runserver
Navigate to http://127.0.0.1:8000/
and chat with your local AI model!
📦 Step 6: Prepare for Deployment
Since Ollama runs only on the host, for now, you cannot deploy it on a cloud platform unless the server supports GPU and Ollama.
Option 1: Docker for Local-Server Deployment
Dockerfile:
FROM python:3.10-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
requirements.txt:
Django>=4.2
requests
.dockerignore and .env as needed.
Run:
docker build -t ollama_django_chatbot .
docker run -p 8000:8000 ollama_django_chatbot
Make sure Ollama is installed and the model is running outside the container.
⚠️ Note on Cloud Deployment
Ollama is currently intended for local use. If you need to deploy this in the cloud:
- Use a dedicated VPS or GPU server (e.g., Paperspace, Lambda Labs).
- Expose Ollama via a private API on your server.
- Secure it with API keys or firewalls.
✅ Bonus: Security and Production Tips
- Use
gunicorn
+nginx
for production deployments. - Restrict access to the Ollama API.
- Use Django’s production settings (
DEBUG=False
, secure keys, etc.).
🎯 Summary
You’ve built a Django web app that:
- Integrates a local LLM (via Ollama).
- Accepts user input and returns AI responses.
- Works locally and is deployable via Docker.