Skip to content

Deploying a Django App

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It includes an ORM, admin interface, and built-in security features, making it a popular choice for building robust, scalable web applications.

This guide explains how to deploy a Django application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.

Prerequisites

  • Python 3.8+
  • pip installed
  • Git and GitHub account
  • Klutch.sh account

Getting Started: Install Django

  1. Create a new directory for your app and set up a virtual environment:
    Terminal window
    mkdir my-django-app
    cd my-django-app
    python3 -m venv venv
    source venv/bin/activate
  2. Install Django:
    Terminal window
    pip install django
  3. Create a new Django project:
    Terminal window
    django-admin startproject mysite .
  4. Run the development server:
    Terminal window
    python manage.py runserver
    Your app should be running at http://localhost:8000.

Sample Code (mysite/views.py)

Add a simple view to your Django project:

from django.http import HttpResponse
def home(request):
return HttpResponse("Hello from Django on Klutch.sh!")

In mysite/urls.py:

from django.urls import path
from .views import home
urlpatterns = [
path('', home),
]

Deploying Without a Dockerfile

  1. Push your Django app to a GitHub repository.
  2. Log in to Klutch.sh.
  3. Create a new project and give it a name.
  4. Create a new app:
    • Select your Django GitHub repository and branch
    • Set the port to route traffic (usually 8000 for Django)
    • Choose region, compute, number of instances, and add any environment variables
  5. Add a start command in your app settings:
    Terminal window
    python manage.py runserver 0.0.0.0:$PORT
    (Klutch.sh will provide the PORT environment variable.)
  6. Add a requirements.txt file with your dependencies:
    Terminal window
    pip freeze > requirements.txt
  7. Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.

Deploying With a Dockerfile

  1. Add a Dockerfile to your project root. Example:
    # Use official Python image
    FROM python:3.11-slim
    # Set working directory
    WORKDIR /app
    # Copy requirements and install dependencies
    COPY requirements.txt ./
    RUN pip install --no-cache-dir -r requirements.txt
    # Copy app source
    COPY . .
    # Expose port (match your Django app)
    EXPOSE 8000
    # Start the app
    CMD ["python", "manage.py", "runserver", "0.0.0.0:$PORT"]
  2. Push your code (with Dockerfile) to GitHub.
  3. In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
  4. Set the service details and environment variables as needed.
  5. Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.

Note: Your Django app should always listen on the PORT environment variable as shown above.


Resources


Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.