django for n00bs

23
Django for n00bs Jen Zajac @jenofdoom [email protected] Django for n00bs

Upload: jen-zajac

Post on 13-May-2015

843 views

Category:

Technology


0 download

DESCRIPTION

Beginner's level overview of the Django web framework, with some c. Presented at Kiwi Pycon 2011

TRANSCRIPT

Page 1: Django for n00bs

Django for n00bs

Jen Zajac @jenofdoom

[email protected]

Django for n00bs

Page 2: Django for n00bs

Django for n00bs

Introduction

Page 3: Django for n00bs

Django for n00bs

What is a framework?

Photo credit: http://www.flickr.com/photos/ikhlasulamal/2331176652/

Page 4: Django for n00bs

Django for n00bs

Frameworks vs CMS's

FRAMEWORKS

• Slower to set up

• Specific application

• Does what you plan it to do

• No wasted features

CMS’s

• Fast to set up

• Broad range

• Requires customising?

• Unwanted overhead

Page 5: Django for n00bs

Django for n00bs

Some backstory

Photo credit: http://www.flickr.com/photos/scottummy/4971054099/

Page 6: Django for n00bs

Django for n00bs

Other frameworks

• Pyramid

• Zope

• Bottle

• TurboGears

http://en.wikipedia.org/wiki/Comparison_of_Web_application_frameworks

Page 7: Django for n00bs

Django for n00bs

So why would I pick Django?

Photo credit: http://www.sxc.hu/photo/495550 (apologies for aspect ratio)

Page 8: Django for n00bs

Django for n00bs

Pros (and some Cons)

• Biggest fish

• Tightly Intergrated Components

• Built-in admin interface

• Documentation

• Release Process

• Authentication & security

Page 9: Django for n00bs

Django for n00bs

MVC: Models, Views, Controllers

Photo credit: http://www.flickr.com/photos/helloturkeytoe/5781708201

Page 10: Django for n00bs

Django for n00bs

Django's MVC model

• In our interpretation of MVC, the “view” describes the data that gets presented to the user. It’s not necessarily how the data looks, but which data is presented. The view describes which data you see, not how you see it. It’s a subtle distinction.

~ The Django FAQ

Page 11: Django for n00bs

Django for n00bs

Setting up a project

Photo credit: http://www.flickr.com/photos/thedailyenglishshow/6013713229/

Disclaimer! The following slides were not in my talk: I did a bit of a live demo instead. So these aren’t 100% complete, just a flavour of

my talk.

Page 12: Django for n00bs

Django for n00bs

It worked!

So, I’m not going to cover installing Django… it’s really easy to get a local set-up, just follow the installation docs on the Django site. When you’ve done that you should end up with this, once you run the test server:

Page 13: Django for n00bs

Django for n00bs

Enabling the admin

Uncomment two lines! (Yes, that’s it)

Page 14: Django for n00bs

Django for n00bsSo, probably we want to create a model now…

So let’s create a model for our movies. This is a really straightforwards model, you can see that it uses some built in Character and Text field types.

Page 15: Django for n00bs

Django for n00bsWe can use our admin interface to add data

Page 16: Django for n00bs

Django for n00bsOK, so we have a model… how users see it?

How do we direct users around our site? We do some URL routing in urls.py

^ This is a regular expression. It’s a simple one! It just catches a number (\d for Digit) and makes it into a name variable which will pass to our view, movie_detail

Page 17: Django for n00bs

Django for n00bs

So what does the view look like?

Here is a really simple view to test we’re getting the url config right, and we’re getting the right variable passed. It goes into views.py

So, does it work?

Yes!

Page 18: Django for n00bs

Django for n00bsWe probably want to display some better info…So, lets make our view a little bit more sophisticated:

(actually, for simple views like these we can actually just wire this call into the urlconf – but with a dedicated view like this we could add in extra processing)

Page 19: Django for n00bs

Django for n00bs

Outputting to a template

So now we want to display enerything in a more sophisticated way, using some html (this is the web after all). In the view in the previous slide, we passed some variables which we can use in our template file:

<- we can stack specific page templates onto a general template

<- we can access the stored different parts of the model within the template

Page 20: Django for n00bs

Django for n00bs

And it should then display

Page 21: Django for n00bs

Django for n00bs

Other Nice Things

• RSS

• Sitemaps

• Tests

• Caching

• Signals

• Custom template tags

• CSV & PDF generation

• Built in pagination

Page 22: Django for n00bs

Django for n00bs

In Conclusion

Photo credit: http://www.flickr.com/photos/wonderferret/306390009/

Page 23: Django for n00bs

Django for n00bs

Resources

http://www.djangobook.com/ Free online version of The Definitive Guide to Django: Web

Development Done Right book by Adrian Holovaty and Jacob Kaplan-Moss

https://code.djangoproject.com/wiki/DjangoResources#TutorialsontheWeb

The Django wiki's collection on links to tutorials available online