Jen Zajac  @jenofdoom [email_address] Django for n00bs
Introduction
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...
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>ht...
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 admi...
MVC: Models, Views, Controllers Photo credit: http://www.flickr.com/photos/helloturkeytoe/5781708201
Django's MVC model <ul><li>In our interpretation of MVC, the “view” describes the data that gets presented to the user. It...
Setting up a project Photo credit: http://www.flickr.com/photos/thedailyenglishshow/6013713229/ Disclaimer! The following ...
It worked! So, I’m not going to cover installing Django… it’s really easy to get a local set-up, just follow the installat...
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 straightforwa...
We can use our admin interface to add data
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.p...
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’r...
We probably want to display some better info… <ul><li>So, lets make our view a little bit more sophisticated: </li></ul>(a...
Outputting to a template <ul><li>So now we want to display enerything in a more sophisticated way, using some html (this i...
And it should then display
Other Nice Things <ul><li>RSS </li></ul><ul><li>Sitemaps </li></ul><ul><li>Tests </li></ul><ul><li>Caching </li></ul><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...
Upcoming SlideShare
Loading in...5
×

Django for n00bs

875

Published on

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

1 Comment
0 Likes
Statistics
Notes
  • You can see my notes for this presentation by clicking on the 'Speakers Notes' tab above.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total Views
875
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide
  • Hi. My name is Jen, you can find me on twitter as @jenofdoom . I work at Catalyst IT as a web developer. I&apos;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&apos;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&apos;t used Python before. I&apos;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&apos;d recommend just taking a basic language tutorial to make sure you&apos;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&apos;m a big fan as it lets me Get Stuff Done.
  • So what, exactly, is a framework? I&apos;m sure most of you know already, but here&apos;s a quick refresher: &amp;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.&amp;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&apos;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&apos;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) -&gt; Pyramid is the result of a merger between repoze.bfg and Pylons, and it&apos;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&apos;s much more bare bones than Django and generally you will be writing some code to glue things together. Zope -&gt; 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 -&gt; Bottle is more of a micro-framework for very small webapps. There are a bunch of these, such as CherryPy, and typically they don&apos;t provide much beyond some basic url dispatching and an http server to build-your-own on top of. TurboGears - &gt; I confess I don&apos;t know that much about TurboGears, but it takes a similar approach to Django in that it is a &apos;full stack&apos; solution, except it&apos;s using open sourced components. It&apos;s now built on top of pyramid I believe.
  • So why would I pick Django over these other frameworks?
  • Biggest fish -&gt; 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 -&gt; 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 -&gt; In terms of wow-factor when you are demoing Django, the built in admin interface is defnietly one of the nicest features. We&apos;re going to have a look in a minute at how easy it is to get this up and running. Documentation -&gt; Django has really nice, pretty, well-written, logical documentation. I really can&apos;t praise it enough. It&apos;s both easy to understand for newcomers and offers plenty of depth for those needing technical information. Well thought our release process -&gt; 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 &amp; security -&gt; Finally, it&apos;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&apos;t want to use it, it&apos;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&apos;t match their intent you need to be be prepared for a little frustration that features you care about won&apos;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&apos; ongoing support of deprecated features through at least a couple of major version changes. Personally I don&apos;t feel like any of these concerns are significant enough to impact my use case, but your mileage may vary.
  • We&apos;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&apos;s approach could more accurately be described as &apos;MTV&apos; (nope, not the channel), which is: Models, Templates, Views. Basically, in Django&apos;s inteterpretation, the &apos;view&apos; 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&apos;ll show you.
  • OK, I think that&apos;s enough bullet points for now, I think it&apos;s about time we got our hands dirty. So here&apos;s an incredibly brief look at getting a project up and running with Django. Ok, so let&apos;s pretend we&apos;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&apos;m not going to cover that. For similar reasons, I won&apos;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&apos;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

    1. 1. Jen Zajac @jenofdoom [email_address] Django for n00bs
    2. 2. Introduction
    3. 3. What is a framework? Photo credit: http://www.flickr.com/photos/ikhlasulamal/2331176652/
    4. 4. 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>
    5. 5. Some backstory Photo credit: http://www.flickr.com/photos/scottummy/4971054099/
    6. 6. 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
    7. 7. So why would I pick Django? Photo credit: http://www.sxc.hu/photo/495550 (apologies for aspect ratio)
    8. 8. 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>
    9. 9. MVC: Models, Views, Controllers Photo credit: http://www.flickr.com/photos/helloturkeytoe/5781708201
    10. 10. 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>
    11. 11. 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.
    12. 12. 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:
    13. 13. Enabling the admin <ul><li>Uncomment two lines! (Yes, that’s it) </li></ul>
    14. 14. 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>
    15. 15. We can use our admin interface to add data
    16. 16. 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
    17. 17. 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!
    18. 18. 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)
    19. 19. 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
    20. 20. And it should then display
    21. 21. 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>
    22. 22. In Conclusion Photo credit: http://www.flickr.com/photos/wonderferret/306390009/
    23. 23. 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>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×