Django Quick Guide: From Setup to Deployment

Last updated 3 months, 3 weeks ago | 217 views 75     5

Tags:- Python Django

Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. It's designed to help developers take applications from concept to launch as quickly as possible.

"The web framework for perfectionists with deadlines."


Key Features of Django

  • Rapid Development

  • Built-in Authentication

  • ORM (Object Relational Mapping)

  • Templating Engine

  • URL Routing

  • ⚙️ Admin Interface

  • Middleware Support

  • Modular Apps


Setting Up Django

✅ Install Django

pip install django

✅ Create a Django Project

django-admin startproject myproject
cd myproject

✅ Run Development Server

python manage.py runserver

Visit: http://127.0.0.1:8000/


Project Structure

myproject/
│
├── manage.py
├── myproject/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
  • manage.py: Command-line utility

  • settings.py: Configuration file

  • urls.py: URL declarations

  • wsgi.py: Web server gateway interface


Creating an App

python manage.py startapp blog

Add 'blog' to INSTALLED_APPS in settings.py.


✍️ Creating Models

# blog/models.py
from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

Run migrations:

python manage.py makemigrations
python manage.py migrate

Admin Panel

Enable admin by registering the model:

# blog/admin.py
from .models import Post
from django.contrib import admin

admin.site.register(Post)

Create admin user:

python manage.py createsuperuser

Access at: http://127.0.0.1:8000/admin/


URL Routing

Project URL Configuration

# myproject/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),
]

App URLs

# blog/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

Views and Templates

View

# blog/views.py
from django.shortcuts import render
from .models import Post

def index(request):
    posts = Post.objects.all()
    return render(request, 'blog/index.html', {'posts': posts})

Template

Create blog/templates/blog/index.html:

<!DOCTYPE html>
<html>
<head><title>Blog</title></head>
<body>
  <h1>My Blog</h1>
  {% for post in posts %}
    <h2>{{ post.title }}</h2>
    <p>{{ post.content }}</p>
  {% endfor %}
</body>
</html>

Forms and User Input

Django Form

# blog/forms.py
from django import forms

class ContactForm(forms.Form):
    name = forms.CharField()
    message = forms.CharField(widget=forms.Textarea)

Handling Form in View

# blog/views.py
from .forms import ContactForm

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            # process data
            pass
    else:
        form = ContactForm()
    return render(request, 'blog/contact.html', {'form': form})

Static Files and Media

Configure in settings.py

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

STATICFILES_DIRS = [BASE_DIR / "static"]
MEDIA_ROOT = BASE_DIR / "media"

In template:

{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">

✉️ Sending Email

Configure in settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.example.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'yourpassword'

Send email:

from django.core.mail import send_mail
send_mail('Subject', 'Message', '[email protected]', ['[email protected]'])

Useful Commands

Command Description
runserver Start dev server
makemigrations Create new migrations
migrate Apply migrations
createsuperuser Create admin user
shell Open Django shell
collectstatic Collect static files
test Run unit tests

Deployment Quick Notes

  1. Set DEBUG = False and add ALLOWED_HOSTS

  2. Use Gunicorn + Nginx or Apache

  3. Configure static/media with collectstatic

  4. Use environment variables for secrets

  5. Consider Docker for containerization


Tips & Best Practices

  • Keep settings.py organized (use .env for secrets)

  • Use Class-Based Views (CBVs) for complex logic

  • Organize static and media files properly

  • Use Django signals for decoupled logic

  • Write tests using pytest or Django’s built-in framework


❌ Common Pitfalls

Issue Solution
Template not found Ensure correct path and app directory
CSRF token missing Always use {% csrf_token %} in forms
Static files not loading Run collectstatic and configure URLs
Admin not accessible Create superuser and add to URLconf
Error 500 Check logs, set proper DEBUG mode

✅ Summary

Django simplifies full-stack web development with batteries-included features like:

  • ORM

  • Admin interface

  • URL routing

  • Forms

  • Templating

You now have a solid understanding to start building and deploying Django applications.


Suggested Next Steps

  • Build a blog or to-do app

  • Learn Django REST Framework (DRF)

  • Explore advanced topics like:

    • Caching

    • Signals

    • Middleware

    • Class-based views

  • Deploy on platforms like Render, Heroku, or DigitalOcean