Django for n00bs
Introduction
Django for n00bs
What is a framework?
Photo credit: http://www.flickr.com/photos/ikhlasulamal/2331176652/
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
Django for n00bs
Some backstory
Photo credit: http://www.flickr.com/photos/scottummy/4971054099/
Django for n00bs
Other frameworks
• Pyramid
• Zope
• Bottle
• TurboGears
http://en.wikipedia.org/wiki/Comparison_of_Web_application_frameworks
Django for n00bs
So why would I pick Django?
Photo credit: http://www.sxc.hu/photo/495550 (apologies for aspect ratio)
Django for n00bs
Pros (and some Cons)
• Biggest fish
• Tightly Intergrated Components
• Built-in admin interface
• Documentation
• Release Process
• Authentication & security
Django for n00bs
MVC: Models, Views, Controllers
Photo credit: http://www.flickr.com/photos/helloturkeytoe/5781708201
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
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.
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:
Django for n00bs
Enabling the admin
Uncomment two lines! (Yes, that’s it)
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.
Django for n00bsWe can use our admin interface to add data
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
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!
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)
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
Django for n00bs
And it should then display
Django for n00bs
Other Nice Things
• RSS
• Sitemaps
• Tests
• Caching
• Signals
• Custom template tags
• CSV & PDF generation
• Built in pagination
Django for n00bs
In Conclusion
Photo credit: http://www.flickr.com/photos/wonderferret/306390009/
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