Getting Started With Django

Anand Reddy Pandikunta

http://avilpage.com/

What You Will Learn

Overview of Django

CRUD Operations

What You Should Know

Basics Of Python

Basics Of Web development

Basics Of Databases

Ask

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 https://bootstrap.pypa.io/get-pip.py | python
deactivate
source bmanager-venv/bin/activate

# Install Django
pip install django==1.7

Starting A Project

django-admin startproject bmanager
python manage.py runserver

Simple Hello World

# views.py
from django.shortcuts import HttpResponse

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

# urls.py
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(datetime.datetime.now())
    return render(request, 'hello.html', {'time': time})

Start App

python3 manage.py startapp bookmarks

MVT Pattern

mvt.png

Models

from django.db import models


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

Admin

from django.contrib import admin

from bookmarks.models import Bookmark


admin.site.register(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