Django Unchained

Mafinar Rashid Khan
Product Manager
Panacea Systems Limited
@mafinar

A Python

Framework for

Perfectionist
with
Deadlines

Python?

A readable, powerful, easy to learn, general purpose programming language with huge base and is based on a vibrant community...

... and framework?

A container of opinions, philosophies, best practices, and taken decisions.

... and perks for following 'em right

Rails, Laravel, Symfony, Play, Django, Grails, Sails,

CakePHP, Flask, Tornado, Sinatra, TurboGears...

In no particular order, and nowhere near complete...

Frameworks are created to fit your brain
Each has its own benefits.
Choose what pleases you

So how does Django fit your brain???

READ the next slide to find out...

You're a Python junky
You like to compose things, modularize things around, you don't like magic, but you don't like repeating yourself either. You value productivity & stability.
You are PYTHONIC
:D

AND YOU ABSOLUTELY LOVE DOCUMENTATIONS!!!
<3

Let's begin now...

So you want to make a web application...
What do you got?

  • You want to be able to talk to a database

  • Make forms and process 'em

  • Perform functions and display things

  • Keep calm and stay DRY

  • It all starts with a URL

  • URL matches with an action

  • Action does its magic

  • Packs it up and dispatches away...

  • Client sees what he/she deserves!

  • You have a Project

  • Your projects have apps

  • Your apps have source code

  • Patterns, patterns everywhere... must. reuse.

How do I create a Django Project or App?

django-admin startproject myproject
django-admin startapp myapp

Introducing the MVT
MODEL, VIEW, TEMPLATE

You just need to know which does what...

And grok the story.. and off you go!!!

# In myproject/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^hello/', 'myapp.views.hello', name='hello'),
)


# In myapps/views.py
from django.http import HttpResponse

def hello(request):
    # Remember those stamps I was talking about?
    return HttpResponse("Hello World!!!")
# In myapp/templates/myapp/hello.html
<html>
    <head><title>{{ title }}</title></head>
    <body>
        Hello, {{ name }}<br />
        Countdown-
        {% for i in countdown %}<p>{{ i }}</p>{% endfor %}
    </body>
</html>
# In myapp/views.py
from django.shortcuts import render

def hello(request):
    name = request.GET.get("name", "Anonymous")
    return render("myapp/hello.html", {"name": name})

You just saw Views and Template out there...

Views can be objects, templates can be inherited...

How about Database?

You may not need SQL as much as you think you would...
(or models.py)

# In myapp/models.py
from django.db import models

class Category(models.Model):
    name = models.CharField(unique=True)
    description = models.TextField(blank=True, 
                                   null=True)

class Product(models.Model):
    name = models.CharField(unique=True)
    category = models.ForeignKey(Category)
    def __unicode__(self):
        return self.name

all_products = Product.objects.all()
sorted_products = Product.objects.all().order_by("-name")
categories = Category.objects.filter(name__istartswith="A")
category = Category.objects.create(name="Electronics", 
                                   description="Zap!")
category.name = "Electronics and Devices"
category.save()
category.delete()
Category.objects.raw("SELECT * FROM myapp_category")

To sum it all up...

I didn't cover Generic Views, FormsClass Based View, Middleware, Signals...

Sorry... :(

But you want Authentication, Admin Panel, Form Binding, Tagging, Comments...

... and I want a pony-tail

Auth, Admin, Comment, Taggit, Actstream, DRF, Crispy Forms, Oscar, Celery, AllAuth...

Who's Using It???

Disqus, Pinterest, Instagram, Mozilla, Wave, Bitbucket... Panacea (Shameless Plug) and lots others...

Can I do "X" with Django??

I am here to answer! Please ask 0:)

THANK YOU

Django Unchained

By Mafinar Khan

Django Unchained

My slides on Django for the Digital World Developers Conference, Dhaka 2015. I aim to explain the work-flow of Django in a story telling fashion as opposed to diving deep unto code (That's reserved for a longer workshoppish session ;))

  • 2,493