Django

Complete deployment guide for Django on NStrim

Overview

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It includes an ORM, admin panel, authentication system, and templating engine built in.

Before You Start
Make sure you have your dashboard open - you'll need the Git credentials displayed there. This template includes Django, Gunicorn, mysqlclient for MySQL support, and Django REST Framework.

Quick Start

1

Create Repository from Template

On your dashboard, go to the Tools & Deployment tab. Click the "New Repository" button, select the Django template from the dropdown, enter a repository name and subdomain, then click "Create Repository".

2

Clone Your Repository

git clone https://<git-server>/<username>/<repo-name>.git
cd <repo-name>
3

Install Dependencies

pip install -r requirements.txt
4

Configure Database & Run Migrations

Update database settings in settings.py (see Database Configuration below), then run:

python manage.py migrate
python manage.py runserver

Server runs on http://localhost:8000 by default.

5

Push Your Changes

git add .
git commit -m "Initial setup"
git push origin main
Every push to Git automatically deploys your application. Check your Module Work URL on the dashboard to see your live site.

Local Development with Docker

If you don't have Python installed locally, you can use Docker to run your Django application.

What is Docker?
Docker creates isolated "containers" with all the software you need (Python, pip) without installing them on your computer.

Running Commands with Docker

# Install dependencies
docker run --rm -v $(pwd):/app -w /app python:3.12 pip install -r requirements.txt

# Run development server
docker run --rm -v $(pwd):/app -w /app -p 8000:8000 python:3.12 sh -c "pip install -r requirements.txt && python manage.py runserver 0.0.0.0:8000"

Database Configuration

The NStrim platform provides you with a MySQL/MariaDB database. Update the DATABASES setting in your settings.py file.

Update settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<username>',
        'USER': '<username>',
        'PASSWORD': '<password>',
        'HOST': 'db.<session-domain>',
        'PORT': '3306',
    }
}
Important
Your database name and username are typically the same as your dashboard username (e.g., student01). Check your dashboard credentials section for exact values.

Project Structure

├── project/
│   ├── settings.py            # Project configuration
│   ├── urls.py                # Root URL configuration
│   └── wsgi.py                # WSGI entry point
├── app/
│   ├── models.py              # Database models
│   ├── views.py               # View functions / classes
│   ├── urls.py                # App URL patterns
│   ├── serializers.py         # DRF serializers
│   ├── admin.py               # Admin panel config
│   └── templates/             # HTML templates
├── static/                    # CSS, JS, images
├── manage.py                  # Django management CLI
├── requirements.txt           # Python dependencies
└── Dockerfile                 # Docker config
        

Common Tasks

Creating a Model

from django.db import models

class User(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

Creating a View

from django.http import JsonResponse
from .models import User

def user_list(request):
    users = list(User.objects.values('id', 'name', 'email'))
    return JsonResponse(users, safe=False)

URL Configuration

from django.urls import path
from . import views

urlpatterns = [
    path('users/', views.user_list, name='user-list'),
]

Useful Commands

python manage.py runserver Start development server
python manage.py migrate Run database migrations
python manage.py makemigrations Create new migrations from model changes
python manage.py createsuperuser Create admin user for /admin panel
python manage.py shell Interactive Python shell with Django

Troubleshooting

OperationalError: (2002) Can't connect to MySQL server

Cause: Database host is incorrect or not accessible.

Solution: Check that HOST in DATABASES setting matches the database host from your dashboard.

OperationalError: (1045) Access denied for user

Cause: Wrong username or password.

Solution: Double-check USER and PASSWORD in your settings.py match your dashboard credentials exactly.

No module named 'MySQLdb'

Solution: Install the MySQL client: pip install mysqlclient. On macOS you may also need: brew install mysql-client.

DisallowedHost: Invalid HTTP_HOST header

Solution: Add your deployment domain to ALLOWED_HOSTS in settings.py: ALLOWED_HOSTS = ['*']

Deployment Workflow

Code Change git push pip install Live!
For more information, visit the official Django Documentation.