53
10 Things You Should Know About Django

10 things you should know about django

  • Upload
    adieu

  • View
    4.621

  • Download
    2

Embed Size (px)

DESCRIPTION

For PyCon China 2014.

Citation preview

Page 1: 10 things you should know about django

10 Things You Should KnowAbout Django

Page 2: 10 things you should know about django
Page 3: 10 things you should know about django

● Python user since 2007● Build web based systems

using Python + Django mostly● Gain interests in infrastructure

gradually● Working on my own startup

since 2014

About Me

twitter: @adieugithub: github.com/adieuwebsite: www.adieu.me

Page 4: 10 things you should know about django

#1

History helps you understand

Page 5: 10 things you should know about django
Page 6: 10 things you should know about django
Page 7: 10 things you should know about django

Adrian Holovaty Jacob Kaplan-MossSimon Willison

Page 8: 10 things you should know about django

The CMS

Page 9: 10 things you should know about django

Main Focus

Fast Development Collaboration

Maintainability Code Reuse

Page 10: 10 things you should know about django

#2

Start with the Basics

Page 11: 10 things you should know about django
Page 12: 10 things you should know about django
Page 13: 10 things you should know about django

The Upward Spiral

Page 14: 10 things you should know about django

It’s just a Web Framework

#3

Page 15: 10 things you should know about django

A basic web serverimport socket

HOST = ''PORT = 80listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)listen_socket.bind((HOST, PORT))listen_socket.listen(1)connection, address = listen_socket.accept()request = connection.recv(1024)connection.sendall("""HTTP/1.1 200 OKContent-type: text/html

<html> <body> <h1>Hello, World!</h1> </body></html>""")connection.close()

Page 16: 10 things you should know about django

WSGI

Page 17: 10 things you should know about django

A mini web frameworkfrom importd import dd(DEBUG=True)

@d("/")def idx(request): return "index.html"

@d("/post/<int:post_id>/")def post(request, post_id): return "post.html", {"post_id": post_id}

if __name__ == "__main__": d.main()

Page 18: 10 things you should know about django

A simple Django site# settings.py

DEBUG = TrueROOT_URLCONF = 'urls'TEMPLATE_DIRS = ['.']

# urls.py

from django.conf.urls.defaults import *

import views

urlpatterns = patterns('', (r'^$', views.index), (r'^test/(\d+)/$', views.test),)

# views.py

from django.shortcuts import render

def index(request): return render(request, 'index.html', {})

def test(request, id): return render( request, 'test.html', {'id': id} )

Page 19: 10 things you should know about django

A single file Django site# Setupfrom django.conf import settingsif not settings.configured: settings.configure( DEBUG = True, ROOT_URLCONF = 'web', TEMPLATE_DIRS = ['.'], )

from django.conf.urls.defaults import patternsurlpatterns = patterns('', (r'^$', 'web.index'), (r'^test/(\d+)/$', 'web.test'),)

# Handlersfrom django.shortcuts import render

def index(request): return render(request, 'index.html', {})

def test(request, id): return render(request, 'test.html', {'id': id})

# Runningif __name__ == '__main__': from django.core.management import execute_from_command_line execute_from_command_line()

Page 20: 10 things you should know about django

● The No. 1 issue web frameworks solve is mapping urls to your handler functions

● Web framework is all about common practice shared by a group of developers

● Choose a web framework when you share the same taste with the author

● Trust the author’s choice and understand why it’s implemented in its way

● Contribute a pull request instead of maintaining a custom fork

Web Framework Basics

Page 21: 10 things you should know about django

It’s just a Python Package

#4

Page 22: 10 things you should know about django

Everything is Python Package

● Django● Third Party Packages● Your Application● Your Site

Page 23: 10 things you should know about django

The default project

Django 1.0 Django 1.7

Page 24: 10 things you should know about django

Recommended project layout

Page 25: 10 things you should know about django

Python Tricks

● virtualenv● pip install -e● python setup.py sdist upload● gdb● monkey patch● pypy

Page 26: 10 things you should know about django

Battery included but replaceable

#5

Page 27: 10 things you should know about django

Batteries

django.dbdjango.formsdjango.templatesdjango.viewsdjango.middlewaredjango.test

django.db.migrationsdjango.contrib.authdjango.contrib.admindjango.contrib.sessionsdjango.contrib.gisdjango.contrib.sites

Page 28: 10 things you should know about django

The bench

Module Package

Template Jinjia2

ORM SQLAlchemy

Routing Rhetoric

Admin django-xadmin

Schema Migration South

Page 29: 10 things you should know about django

Postgres is your friend

#6

Page 30: 10 things you should know about django
Page 31: 10 things you should know about django

● django.contrib.postgres● django.contrib.gis● django-hstore● django-tenant-schemas● django-pgjson● django-postgrespool● django-ext-pgarray

Postgres is the community choice

Page 32: 10 things you should know about django

Performance is not your priority

#7

Page 33: 10 things you should know about django

The slogan

The old one:The web framework for perfectionists with deadlines.The new one:Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

Page 34: 10 things you should know about django

Performance is not a problem if you use it right

Page 35: 10 things you should know about django

Performance Improvements

django-debug-toolbarGeventGunicornCacheDB tweaksApplication server farms

Page 36: 10 things you should know about django

Choose Django for the Ecosystem

#8

Page 37: 10 things you should know about django
Page 38: 10 things you should know about django
Page 39: 10 things you should know about django
Page 40: 10 things you should know about django
Page 41: 10 things you should know about django
Page 42: 10 things you should know about django

Typical Django workflow

Need a new feature

Existing Package

requirements.txt

search integrate

Fork

patchpull request

Page 43: 10 things you should know about django

Know the Limitations

#9

Page 44: 10 things you should know about django

Coding Style

Page 45: 10 things you should know about django

Realtime

Page 46: 10 things you should know about django

Performance Critical

Page 47: 10 things you should know about django

Prepare for the Future

#10

Page 48: 10 things you should know about django

Restful API with multiple platform

Page 49: 10 things you should know about django

SQL + NoSQL = ❤

Page 50: 10 things you should know about django

A bright future

django

django-xadmin

django-rest-framework

django-debug-toolbar

Page 51: 10 things you should know about django

Ready for the Open Source future?

[email protected]

Page 52: 10 things you should know about django

Linkshttps://www.djangoproject.com/

http://www.ellingtoncms.com/

http://www.w3schools.com/

https://www.djangopackages.com/

http://learnpythonthehardway.org/book/

http://www.jeffknupp.com/blog/2014/03/03/what-is-a-web-framework/

https://github.com/amitu/importd

http://softwaremaniacs.org/blog/2011/01/07/django-micro-framework/en/

http://jinja.pocoo.org/docs/dev/

http://www.sqlalchemy.org/

https://github.com/avanov/Rhetoric

https://github.com/sshwsfc/django-xadmin

http://south.aeracode.org/

https://www.kickstarter.com/projects/mjtamlyn/improved-postgresql-support-in-django

https://github.com/djangonauts/django-hstore

https://github.com/bernardopires/django-tenant-schemas

https://github.com/djangonauts/django-pgjson

https://github.com/kennethreitz/django-postgrespool

https://github.com/niwibe/djorm-ext-pgarray

https://github.com/django-debug-toolbar/django-debug-toolbar

http://www.gevent.org

http://gunicorn.org/

https://www.djangopackages.com/

http://www.django-rest-framework.org/

http://www.postgresql.org/message-id/[email protected]

Page 53: 10 things you should know about django

Thank You