COMP249 Web Technology: Django Notes
Firstly let me apologise for the late notice regarding cancellation of the guest lecture. I was unable to contact Malcolm and it now seems that he has had some personal issues that have meant he is out of email contact. These notes and the accompanying video lecture are intended to replace what Malcolm might have said.
What is Django
Django is a Python based web framework that provides a base on which to build modern web applications. Instead of having to do everything yourself, Django provides ways to manage common requirements like sessions, user authentication, databases, page templating etc. Django makes developing a simple web application a much easier and faster job. I will try to demonstrate this by writing a version of the bookmarker application in Django in a video.
Django is not the only such framework. For Python there are a number of others, probably most notable is TurboGears. In many ways the ancestor of this kind of package is Ruby on Rails which is based on the functional language Ruby. Rails was a bit of a revolution in terms of web frameworks a few years ago when it introduced the idea of a framework that wrote boilerplate code for you and made it easy to develop simple database backed web applications. PHP could also be considered a player in this space but plain PHP is more like plain Python - the tools are there but you put them together however you want. A better comparison would be the Zend Framework which is an object oriented framework "developed with the goal of simplifying web development while promoting best practices in the PHP developer community."
Django provides the following important components:
- An Object Relational Mapper - a database interface that presents data to your program as objects meaning you never need to write SQL.
- A Model-View-Template architecture. Models define the structure of your data, Views define how data will be presented or how it can be manipulated and Templates define how the web pages will look. (Note this is similar to the well known Model-View-Controller pattern in Software Engineering).
- A user management and session subsystem. You don't need to do very much work to allow logins to your system and keep track of user sessions.
- A rich administrative interface for your database. This allows you to add, edit or delete data from your database without writing any code.
- A library of modules to support common tasks in building web applications.
Sessions might be worth highlighting since you have been implementing these yourself lately. Every Django application tracks users via a session cookie that it stores in a session table just like you did. However, this is done behind the scenes and you don't need to do anything in your code to enable this. A session is created for every visitor, even if they aren't logged in. In your application you can make use of sessions by storing information in the users session object. For example, you might want to record the addition of an item to a shopping cart - you can just add it to the session object rather than having to create a specific database model. The next time that same user makes a request, the same session object will be available to you. There is no need to send cookies yourself, everything is managed for you. This is common to most application frameworks and is one reason why some experienced web programmers don't know what cookies are - they never had to deal with them; hopefully you will be wiser than them!
Video
Watch this Django screencast by Ian Millington showing how to create a simple blog application in a few minutes using Django. While lots of the detail is not explained it shows how easy it is to put together a simple application. Note the following points:
- The database is specified entirely in terms of definitions of
classes in the models.py file. These classes get translated to
SQL tables. Instead of performing an SQL query, you make calls
like
BlogEntry.objects.all()orBlogEntry.objects.sort('-date')which perform the query and return a sequence ofBlogEntryobjects. - The way that Django works out what procedure to call based on
the URL of the request. Unlike with our CGI scripts, a Django
application handles many URIs. Instead of the server routing
requests to scripts based on thier path name, routing is done
based on regular expressions in the
urls.pyfile. This means that it's possible to design your URIs more easily; it also implies that Django applications will be more tightly integrated with the server than a CGI script. - The use of templates to generate HTML. Similar to the way I did this in your assignment, the template file defines the outline of the HTML pages to be generated. Django templates are obviously much richer than my simple version. They are also richer than is shown in this video; in particular, they support inheritance so that you can define one site-wide template with your page design and then specialise this to add page content of specialist pages.
If you are interested, there are other screencasts about Django that you could watch. You could try out developing your own project by following the tutorial on the main Django site - or even just read through it to see the kinds of things that can be done.