Hi. My name is Jen, you can find me on twitter as @jenofdoom . I work at Catalyst IT as a web developer. I'm here today to talk to you about the Django web framework at quite an introductory level, i.e. for n00bs.
There are two potential audiences for this talk: those of you who are familar with Python and have used other frameworks but don't have any experience with Django, and are looking to simply learn what extra Django might offer; and those of you who might have used other frameworks or content mangement systems but haven't used Python before. I'm going to try and cater for both of those groups in this talk (possibly focusing on slightly on the later), but of course the caveat of this is that I may not go into quite the same level of detail that I would be able to if I was targeting one group over the other, so if you have any questions please feel free to ask them at the end. I personally feel that if you are new to Python as a language, as long as you have some experience with a programming language of some description and you understand basic control flow stuctures, Django is an excellent gateway drug to serve as introduction to Python for the web, as it does a great job of handling a lot of the intial futzing around with configuration and allowing you to just get on with writing code that does stuff, quickly. There are plenty of great resources for picking up Python available online and I'd recommend just taking a basic language tutorial to make sure you've got the concepts down before starting with Django. I myself am a long way from being a Python expert of any description, but I do use it for my day job and I'm a big fan as it lets me Get Stuff Done.
So what, exactly, is a framework? I'm sure most of you know already, but here's a quick refresher: &quot;A web application framework is a software framework that is designed to support the development of dynamic websites, web applications and web services. The framework aims to alleviate the overhead associated with common activities performed in Web development. For example, many frameworks provide libraries for database access, templating frameworks and session management, and they often promote code reuse.&quot; source: the ever ubiqutous wikipedia http://en.wikipedia.org/wiki/Web_framework Really though, putting this quote aside, I feel like this picture does a pretty good job of explaining what a framework is – it’s a basic structure that you can then build upon further.
Some of you may be more familiar with the concept of a Content Management System when you think about web applications, such as Drupal or Joomla etcetera. These CMS's have great advantages when it comes to getting up and running and solving common functionality needs quickly, but if you are doing something really bespoke they are definitely lacking, requiring a lot of wrangling to make them do what you want to do, and using up a lot of additional overhead for functionality you may not find of use. Conversly, a framework is a lot more bare bones. You are going to have to do a bunch more work to get up and running, but because you are putting in only the code that is relevant to your aims, you have a lot more flexibility and hopefully you will end up with a much leaner application.
Django originated at the Lawrence Journal-World newspaper in 2003. In the summer of 2005 it was released as an open source project. Yes, it is named after Django Reinhart, the guitarist. Django took a big step forwards in maturity with the release of version 1.0 back in 2008, and it's now up to version 1.3 with a major release roughly once a year.
Of course, Django is not the only web framework. Ruby on Rails is a big fish of course, and on the Python side of things we have not only Django but also a handful of other frameworks. Briefly, some of these are: Pyramid (Pylons) -> Pyramid is the result of a merger between repoze.bfg and Pylons, and it's a lightweight framework that offers very good performance plus a modular approach to choosing for example, if you want routes or url traversal, or if you want mako or chameleon templates. It's much more bare bones than Django and generally you will be writing some code to glue things together. Zope -> Zope has been around for quite a long time and has branched of into a bunch of different project such as BlueBream. Zope includes ZODB, an object database for Python, plus a bunch of standard libraries. Bottle -> Bottle is more of a micro-framework for very small webapps. There are a bunch of these, such as CherryPy, and typically they don't provide much beyond some basic url dispatching and an http server to build-your-own on top of. TurboGears - > I confess I don't know that much about TurboGears, but it takes a similar approach to Django in that it is a 'full stack' solution, except it's using open sourced components. It's now built on top of pyramid I believe.
So why would I pick Django over these other frameworks?
Biggest fish -> Perhaps this seems a bit self-evident, but one major advantage is that Django is pretty popular. More users means more people to ask for help if you get stuck. It also means there is a nice big community of folks writing tutorials and creating add-ons. Tightly Intergrated Components -> Django takes a very much all-in-one, batteries-included type of approach. The url dispatcher, object realational model, template system etcetra all come as part of the package, and they are all specific to Django. The great advantage this offers is that everything meshes together really well. Admin interface -> In terms of wow-factor when you are demoing Django, the built in admin interface is defnietly one of the nicest features. We're going to have a look in a minute at how easy it is to get this up and running. Documentation -> Django has really nice, pretty, well-written, logical documentation. I really can't praise it enough. It's both easy to understand for newcomers and offers plenty of depth for those needing technical information. Well thought our release process -> Django does a really good job of giving you plenty of advance notice about what features are going to be changed or deprcated two or three major releases away. The release notes offer solid advice about what you need to do to get your existing apps up to date. Authentication & security -> Finally, it's great to have all your user authentication and permissioning taken care of out of the box, as well as CRSF protection. As with a bunch of the features mentioned above, if you don't want to use it, it's realy easy to turn off and substitute something else. Of course, for some people and some situations, some of these upsides could be considered as downsides. There has been some criticism leveled at Django with regards to the amount of its functionality that was created from scratch as opposed to using already established open source componements. This could in theory take folks away from the established components, and generally reduces interoperability. There is essentially only a small core group of people that actually guide Django, and there is some feeling that if your thoughts don't match their intent you need to be be prepared for a little frustration that features you care about won't get folded in. Right now Django is Python 2.x only. It seems likely that eventually Django will support Python 3, but it could be a long time coming due to their release process' ongoing support of deprecated features through at least a couple of major version changes. Personally I don't feel like any of these concerns are significant enough to impact my use case, but your mileage may vary.
We're just going to pause for a second here before we dive into setting up a project. Hopefully most of you are already familiar with the MVC model, which most Python based frameworks use. Briefly, when we lay out an application in an Models-Views-Controllers way, we split up the application into layers, keeping presentation seperate from the program logic, and keeping that separate from the data model.
One gotcha to those of you coming from other MVC frameworks is that Django has a slightly different interpretation of what MVC means. Django's approach could more accurately be described as 'MTV' (nope, not the channel), which is: Models, Templates, Views. Basically, in Django's inteterpretation, the 'view' describes the data that gets presented to the user, i.e. it is a Python callback function for a particular URL. Then all the presentation gets seperated out into the templates, as I'll show you.
OK, I think that's enough bullet points for now, I think it's about time we got our hands dirty. So here's an incredibly brief look at getting a project up and running with Django. Ok, so let's pretend we're creating a simple website for a group of friends to be able to log in and vote on movies that they want to see when they next hang out, to save a bunch of arguements on the night.
Right, so getting Django set up locally for development is pretty straightforwards, you can follow the instructions on the Django site to get in installed so I'm not going to cover that. For similar reasons, I won't cover initial project setup as I want to just jump straight in to actaully making a site just to give you a taster of that.
I wanted to cover forms as wel,, but alas no time!
I love frameworks because they allow me to build a project hey way I want it to be built. I'm a big fan of Django in particular because it’s so quick to get up and running. My first introduction to Django was a 15 minute youtube video which showed someone putting together a simple wiki system. This talk is my attempt to pay forward some of the excitement I felt watching that initial demo. I hope that your have found it useful.
Django for n00bs
Jen Zajac @jenofdoom [email_address] Django for n00bs
What is a framework? Photo credit: http://www.flickr.com/photos/ikhlasulamal/2331176652/
Frameworks vs CMS's <ul><li>FRAMEWORKS </li></ul><ul><li>Slower to set up </li></ul><ul><li>Specific application </li></ul><ul><li>Does what you plan it to do </li></ul><ul><li>No wasted features </li></ul><ul><li>CMS’s </li></ul><ul><li>Fast to set up </li></ul><ul><li>Broad range </li></ul><ul><li>Requires customising? </li></ul><ul><li>Unwanted overhead </li></ul>
Some backstory Photo credit: http://www.flickr.com/photos/scottummy/4971054099/
Other frameworks <ul><li>Pyramid </li></ul><ul><li>Zope </li></ul><ul><li>Bottle </li></ul><ul><li>TurboGears </li></ul>http://en.wikipedia.org/wiki/Comparison_of_Web_application_frameworks
So why would I pick Django? Photo credit: http://www.sxc.hu/photo/495550 (apologies for aspect ratio)
Pros (and some Cons) <ul><li>Biggest fish </li></ul><ul><li>Tightly Intergrated Components </li></ul><ul><li>Built-in admin interface </li></ul><ul><li>Documentation </li></ul><ul><li>Release Process </li></ul><ul><li>Authentication & security </li></ul>
Django's MVC model <ul><li>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. </li></ul><ul><li>~ The Django FAQ </li></ul>
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.
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:
Enabling the admin <ul><li>Uncomment two lines! (Yes, that’s it) </li></ul>
So, probably we want to create a model now… <ul><li>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. </li></ul>
OK, so we have a model… how users see it? <ul><li>How do we direct users around our site? We do some URL routing in urls.py </li></ul>^ 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
So what does the view look like? <ul><li>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 </li></ul>So, does it work? Yes!
We probably want to display some better info… <ul><li>So, lets make our view a little bit more sophisticated: </li></ul>(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)
Outputting to a template <ul><li>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: </li></ul><- we can stack specific page templates onto a general template <- we can access the stored different parts of the model within the template
Other Nice Things <ul><li>RSS </li></ul><ul><li>Sitemaps </li></ul><ul><li>Tests </li></ul><ul><li>Caching </li></ul><ul><li>Signals </li></ul><ul><li>Custom template tags </li></ul><ul><li>CSV & PDF generation </li></ul><ul><li>Built in pagination </li></ul>
In Conclusion Photo credit: http://www.flickr.com/photos/wonderferret/306390009/
Resources <ul><li>http://www.djangobook.com/ </li></ul><ul><li>Free online version of The Definitive Guide to Django: Web Development Done Right book by Adrian Holovaty and Jacob Kaplan-Moss </li></ul><ul><li>https://code.djangoproject.com/wiki/ </li></ul><ul><li>DjangoResources#TutorialsontheWeb </li></ul><ul><li>The Django wiki's collection on links to tutorials available online </li></ul>