Getting Started With Django

Anand Reddy Pandikunta

What You Will Learn

Overview of Django

CRUD Operations

What You Should Know

Basics Of Python

Basics Of Web development

Basics Of Databases


Feel free to interrupt & ask during the session.

Django Setup

# Create New Virtualenv
python3 -m venv bmanager-venv --without-pip
source bmanager-venv/bin/activate
curl | python
source bmanager-venv/bin/activate

# Install Django
pip install django==1.7

Starting A Project

django-admin startproject bmanager
python runserver

Simple Hello World

from django.shortcuts import HttpResponse

def hello(request):
    return HttpResponse('Hello World')

url(r'^hello', hello, name='hello')

The BIG Picture

django-1.png django-2.png django-3.png django-4.png

Better Hello World

import datetime
from django.shortcuts import render

def hello(request):
    time = str(
    return render(request, 'hello.html', {'time': time})

Start App

python3 startapp bookmarks

MVT Pattern



from django.db import models

class Bookmark(models.Model):
    name = models.CharField(max_length=20)
    url = models.URLField()


from django.contrib import admin

from bookmarks.models import Bookmark

Models API

>>> from bookmarks.models import Bookmark

>>> Bookmark.objects.all()
[<Bookmark: python>, <Bookmark: django>]

>>> Bookmark.objects.get(pk=1)
<Bookmark: python>

>>> Bookmark.objects.filter(name__startswith='p')
[<Bookmark: python>, <Bookmark: python>]

Create Bookmark View

Create View

Create Form

Create Template

More Views

List Bookmarks

Edit Bookmarks

Delete Bookmarks

Adding a Foreignkey

class Folder(models.Model):
    folder = models.CharField(max_length=20)

class Bookmark(models.Model):
    folder = models.ForeignKey(Folder, null=True)

Where To Go From Here

Where To Get Help