CodeToLive

Introduction to Django

Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. It follows the "batteries-included" philosophy and provides many features out of the box.

What is Django?

Django is a free, open-source web framework written in Python that follows the model-template-views (MTV) architectural pattern. It was created to help developers take applications from concept to completion as quickly as possible.

Key Features of Django

  • Rapid Development: Django was designed to help developers take applications from concept to completion as quickly as possible.
  • Secure: Django takes security seriously and helps developers avoid many common security mistakes.
  • Scalable: Django uses a component-based "shared-nothing" architecture.
  • Versatile: Django can be used to build almost any type of website.
  • Batteries Included: Comes with many built-in features like admin interface, ORM, authentication, etc.

Django Architecture (MTV)

Django follows the Model-Template-View architectural pattern:

  • Model: The data access layer containing everything about the data
  • Template: The presentation layer that handles how the data is presented
  • View: The business logic layer that accesses the model and renders the template

Setting Up Django

To get started with Django, you'll need to have Python installed. Then install Django using pip:


# Install Django
pip install django

# Verify installation
python -m django --version
                

Creating a Django Project

Start a new Django project with this command:


django-admin startproject mysite
                

This will create a directory structure like this:


mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
                

Running the Development Server

To verify your Django project works, run the development server:


python manage.py runserver
                

Then visit http://127.0.0.1:8000/ in your browser. You should see the Django welcome page.

Creating a Django App

Django projects are made up of multiple apps. Create your first app:


python manage.py startapp myapp
                

This creates a directory structure like:


myapp/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py
                

Registering the App

Add your app to the INSTALLED_APPS in settings.py:


# mysite/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',  # Add your app here
]
                

Creating Your First View

Edit myapp/views.py:


from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the myapp index.")
                

URL Configuration

Create a urls.py file in your app directory:


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

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

Then include these URLs in your project's urls.py:


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

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

Django Admin Interface

Django comes with a built-in admin interface. First create a superuser:


python manage.py createsuperuser
                

Then run the server and visit http://127.0.0.1:8000/admin/ to access the admin site.

Django's Built-in Features

Django includes many built-in features:

  • Authentication: User authentication system
  • Admin Interface: Automatic admin interface
  • ORM: Database access through Python code
  • Forms: Tools for building and validating forms
  • Security: Protection against common vulnerabilities
  • Internationalization: Support for multiple languages

Django Project Structure

A typical Django project has this structure:


project/
    manage.py
    project/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
    app1/
        migrations/
        __init__.py
        admin.py
        apps.py
        models.py
        tests.py
        views.py
    app2/
        ...
    static/
    templates/
                

Django's Design Philosophies

  • Loose Coupling: Different layers shouldn't "know" about each other
  • Don't Repeat Yourself (DRY): Every piece of knowledge should have a single representation
  • Quick Development: Django should enable fast development
  • Clean Design: Django should maintain clean code throughout

When to Use Django

Django is particularly good for:

  • Content management systems
  • News sites
  • Social networks
  • Scientific computing platforms
  • Any database-driven website

Popular Sites Using Django

  • Instagram
  • Pinterest
  • Mozilla
  • Disqus
  • The Washington Times
  • NASA
Next: Models & Databases