django for n00bs
DESCRIPTION
Beginner's level overview of the Django web framework, with some c. Presented at Kiwi Pycon 2011TRANSCRIPT
![Page 2: Django for n00bs](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/2.jpg)
Django for n00bs
Introduction
![Page 3: Django for n00bs](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/3.jpg)
Django for n00bs
What is a framework?
Photo credit: http://www.flickr.com/photos/ikhlasulamal/2331176652/
![Page 4: Django for n00bs](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/4.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/5.jpg)
Django for n00bs
Some backstory
Photo credit: http://www.flickr.com/photos/scottummy/4971054099/
![Page 6: Django for n00bs](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/6.jpg)
Django for n00bs
Other frameworks
• Pyramid
• Zope
• Bottle
• TurboGears
http://en.wikipedia.org/wiki/Comparison_of_Web_application_frameworks
![Page 7: Django for n00bs](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/7.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/8.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/9.jpg)
Django for n00bs
MVC: Models, Views, Controllers
Photo credit: http://www.flickr.com/photos/helloturkeytoe/5781708201
![Page 10: Django for n00bs](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/10.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/11.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/12.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/13.jpg)
Django for n00bs
Enabling the admin
Uncomment two lines! (Yes, that’s it)
![Page 14: Django for n00bs](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/14.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/15.jpg)
Django for n00bsWe can use our admin interface to add data
![Page 16: Django for n00bs](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/16.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/17.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/18.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/19.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/20.jpg)
Django for n00bs
And it should then display
![Page 21: Django for n00bs](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/21.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/22.jpg)
Django for n00bs
In Conclusion
Photo credit: http://www.flickr.com/photos/wonderferret/306390009/
![Page 23: Django for n00bs](https://reader031.vdocuments.net/reader031/viewer/2022012918/5552bfcab4c905920f8b476f/html5/thumbnails/23.jpg)
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