django: beyond basics

44
Django Beyond Basics

Upload: arunvr

Post on 10-May-2015

1.096 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Django: Beyond Basics

Django Beyond Basics

Page 2: Django: Beyond Basics

Who is this for?

Page 3: Django: Beyond Basics

NOOB GOOD CHUCK NORRIS

What I thought…

Page 4: Django: Beyond Basics

NOOB GOOD CHUCK NORRIS

It is more like… DESIGNER

BIG PICTURE GUY OPERATIONS

ADMIN

RAILS GUY

Page 5: Django: Beyond Basics

@arocks arunrocks.com

Hi!

Page 6: Django: Beyond Basics

Disclaimer This presentation does not aim to be a

comprehensive overview of any part of Django. There are several tutorials covering various

aspects of the framework. Instead this is talk is part experience sharing, part advocacy and

part entertainment.

Page 7: Django: Beyond Basics

What is Django?

Page 8: Django: Beyond Basics

It is just a framework!

Page 9: Django: Beyond Basics

Definitely not a CMS!

Page 10: Django: Beyond Basics

Get the BIG picture

Page 11: Django: Beyond Basics

The BIG picture slide

Thanks to Karen Rustad & Asheesh Laroia

Page 12: Django: Beyond Basics

Also starring…

• Lightweight, standalone web server for dev • Form serialization and validation system • Flexible caching framework • Support for middleware classes at various levels • Internationalization system • Unit test framework

Page 13: Django: Beyond Basics

Why is Django Awesome?

Page 14: Django: Beyond Basics

List of awesome-ness

• Admin • Security • Great documentation • Friendly community • Stable • Batteries included • Open Source!

Page 15: Django: Beyond Basics

Coming from PHP/ASP background

Page 16: Django: Beyond Basics

Coming from PHP/ASP background

Step 1: Forget Everything, esp how easy life was… Step 2: Think architecture first Step 3: Think about Separation of Concerns Step 4: ??? Step 5: Profit!!!

Page 17: Django: Beyond Basics

101 bad excuses not to use Django

Page 18: Django: Beyond Basics

But Django is too heavy!

Page 19: Django: Beyond Basics

Flask from flask import Flask

app = Flask(__name__)

@app.route('/')

def hello_world():

return 'Hello World!'

if __name__ == '__main__':

app.run()

Django example is one file & almost the same size!

Django from django.http import HttpResponse

from django.conf.urls.defaults import patterns

DEBUG=True

ROOT_URLCONF = 'pico'

DATABASES = { 'default': {} }

SECRET_KEY = '0123456789‘ * 50

def index(request):

return HttpResponse('Hello World!')

urlpatterns = patterns('', (r'^$', index))

$ PYTHONPATH=. django-admin.py runserver

0.0.0.0:8000 --settings=pico

Page 20: Django: Beyond Basics

No, It is batteries included!

(+ cool downloadable apps)

Page 21: Django: Beyond Basics

But Django is too ugly!

Page 22: Django: Beyond Basics

Why use: “example.com/product/[0-9]+”

?

Isn’t this is much cooler: “example.com/product/{id}”

? SQL Injection!

Page 23: Django: Beyond Basics

example.com/product/1 or 1=1 will become:

SELECT * FROM Products WHERE ID = 1 OR 1=1

Page 24: Django: Beyond Basics

Even Django will not always save you: “example.com/product/(.+)”

Avoid!

Page 25: Django: Beyond Basics

Be as strict as possible: “example.com/product/[0-9]+”

Page 26: Django: Beyond Basics

Looks prettier now, huh?

Page 27: Django: Beyond Basics

Why not Django?

• Unusually high performance needs • Existing Database models? • Migrations? • ORM/ Template is not enough

In other words, you want to replace all of Django’s components. Which you would eventually?!

Page 28: Django: Beyond Basics

Best Practices

• Distrust outside data. Sanitize everything! • Don’t leak implementation details. • Fatter Models/Managers and Leaner Views • Follow PEP8 and readable names • Be as DRY as possible. • Break down into reusable Apps

Page 29: Django: Beyond Basics

novice questions

What is a QuerySet? Why is media separate?

Which IDE? How to deploy?

Page 30: Django: Beyond Basics

Must-learn Python Packages

Page 31: Django: Beyond Basics

Must-learn Python Packages

• Pip – Don’t start without this!

• iPython/BPython – Better than vanilla console

• Pudb – Best debugger

• Fabric – Easy deployment

Page 32: Django: Beyond Basics

But what goes well with Django?

Page 33: Django: Beyond Basics

Must-learn Django Packages

Page 34: Django: Beyond Basics

Must-learn Django Packages

• Django-debug-toolbar – Only in DEV! • Django_compressor – Not just compression • Django-extensions – Tons of goodies • South – Getting integrated? • Celery – Delayed Gratification • Tastypie *– Build yummy APIs

* Or anything that suits you

Page 35: Django: Beyond Basics

Other cool Django Packages

• Django social auth: One app to most Social logins • Django Paypal: PayPal Payments Standard & Pro • crispy-forms: Nice HTML for forms • django-taggit: Implement tags easily • Psycopg2: Talk to PostgreSQL, a solid database • django-storages: Store anywhere - Cloud, DB or FTP

Page 36: Django: Beyond Basics

My Django Workflow

Page 37: Django: Beyond Basics

1) Create a new Django project 2) Find a 3rd party app or create an app 3) Write/Improve models.py 4) Play with queries on console. Run syncdb. 5) Add a bare admin.py 6) Add data from admin UI 7) Write views.py. Leverage CBVs 8) If needed, add a model form to forms.py 9) Add views to urls.py 10) Jump to step 3 till app looks good 11) jump to step 2

For examples, head to arunrocks.com

A simplistic Django workflow…

Page 38: Django: Beyond Basics

1) Create a new Django project 2) Find a 3rd party app or create an app 3) Write/Improve models.py 4) Play with queries on console. Repeat step 3 5) Add a bare admin.py 6) Add data from admin UI 7) Write views.py. Leverage CBVs 8) If needed, add a model form to forms.py 9) Add views to urls.py 10) Jump to step 3 till app looks good

Make friends with Git, South, Fabric…

A better Django workflow…

$ ./manage.py schemamigration app --initial

$ ./manage.py migrate app

$ ./manage.py schemamigration app --auto

$ git init

Write tests.py

Fabric/Puppet/Chef

Page 39: Django: Beyond Basics

Forms are easy!

Page 40: Django: Beyond Basics

Forms are easy!

• Use forms as much as possible (Hint: security) • ModelForms cover most uses case • But select which fields show in ModelForms • Hard set all defaults before form.save • FormView is a great generic view for forms • Using bootstrap? Use crispy-forms to save time

Page 41: Django: Beyond Basics

Should I use CBVs?

Page 42: Django: Beyond Basics

Ok, I made a Django site. Now what?

Page 43: Django: Beyond Basics

Ok, I made a Django site. Now what?

• Turn off DEBUG • Use HTTPS logins • Set X-Frame-

Options header • Use SESSION_COOKIE_

SECURE • Change /admin/ url Or easier, go to

http://ponycheckup.com/

Page 44: Django: Beyond Basics

@arocks