Back to Main Page

Overview Edit

Django is a Python framework that is easy to learn and suited for simple, practical designs of a database driven Web application. It comes with many facilities such as an object-relational mapper, an automatic administrator interface, an easy-to-use URL design feature, a powerful template system, a cache system, and support for international languages. For this project, we focused primarily on the front-end of Django, which includes a URL design feature, and views using the template system.

Framework Structure Edit

Django stresses the idea of decoupling the many aspects of the framework. A philosophy similar to MVC is used for splitting the components of the framework. The basic components of Django are the model, URL, view and template. We focus on the URL, view and template aspects.

Views Using Django Template System Edit

Template Features Edit

Templates are called by views to render html to display in the browser. A template can simply be an HTML file but its strength comes from the markup tags that can be placed in the file to perform simple looping and other data displaying tasks. The philosophy that Django designers feel strongly about is that making templates is only for the presentation of a webpage, not programming. Variables cannot be set and no Python code can be run from a template. Templates are meant for web designers, not programmers. The experience of making templates should be similar to coding HTML. Django designers feel that syntax should not be too strict as to create limitations on the web designer.

Inheritance Edit

If the programmer includes {% extends "base.html" %} at the very top of a template, Django will use the blocks within the child template to fill in the blocks defined in the base template, thereby cutting down on redundancy. This is a very important feature of Django because it means that things like the appearance of a web page can be decoupled from the page's content. Instead of having to change the child template which contains the data, Django allows the programmer to just change how the base parent template displays the blocks, which will then be automatically filled with information from the child. This template system means the programmer can easily change how the site looks by changing the base.html file, without changing anything else.

Template utilities Edit

Substitution - Django will substitute the value of anything within doubly curly braces into the HTML.

Logical tags - Django allows you to use such constructs as if/else and for loops to conditionally display content.

Filters - Django uses a Unix style pipe to allow filtering values into a specified format. The programmer can use multiple filters and make custom filters too. Examples of filters are.

  • current_date|date:"h:i:sa T, F j, Y": This will take the variable current_date and display it in the format hours:minutes:seconds am/pm, Month Day, Year.
  • word{object_list.count|pluralize}: This takes word and depending on the value of object_list.count, word is pluralized to be words or remains as word.

Filters are effectively functions.

URL Parsing Edit

In order to know when to call which view, the URL is parsed. One or many URL configuration files may exist for a given project. One of the most intriguing feature of Django, is that URLs are parsed by matching strings using regular expressions which provides a wide range of possibilities for determining which views to call based on the URL. This will be explained more later on.

Django Example - Web Survey Edit


Title Page

To demonstrate how Django works we look at the example of an online survey with multiple choice questions.

There are 2 models used in a survey: questions and answers. With Django, we have the choice of whether we would like to design the view or the URL first. For our survey we want the following pages:

  • A title page from which we can begin the survey.
  • A page displaying a form for the question and its answer choices.

    Question Detail Page

  • A page displaying all the survey results.

    Results Page

This amounts to 3 different views. We will let the index view process the title page, the question detail view display the question, and the result view display the survey results. The view functions are responsible for request and data processing. We still need some code to actually store data from the forms on the question detail view to the database. To do this we create a vote view which stores the input into our models.

We now need views to be called by specific URLs. We set up our URL configuration file which links URLs to views.

If our website is, we can use the following matchings.

  • calls the index view.
  • calls the question detail view, which in this case will display the 1st question.
  • calls the vote view, which in this case will be for the 2nd question.
  • calls the results view.

The python code for matching the URLs is:

 urlpatterns = patterns(,
     (r'^polls/$', 'mysite.polls.views.index'),
     (r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'),
     (r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'),
     (r'^polls/(?P<poll_id>\d+)/vote/$', ''),
 )  #What is,views.results  here???

In total there are 4 URL matchings. Each question has its own URL, and even if there were 8 questions, we would still only need 4 patterns, since each URL is matched to a URL pattern, which then invokes a specific view. This is a unique feature of Django which will be touched upon in the unique features section.

Unique Features Edit

Built-in Admin Interface Edit


Main Admin Page

One of the Django's most useful features is the automatically generated administrator interface for models. This feature is great because it reduces the amount of coding required by the programmer by specially creating these pages for an administrator role. The programmer enables this interface by simply adding the admin application to the website's setting file. If the programmer had previously created a superuser and password, the administrator account defaults to those settings.

Within this interface, the administrator can edit the content of the site, including the groups, users, and sites, which are the default Django features. A site developer's models can be displayed on the admin page if the programmer writes an file telling the the application how to display and handle the model. Once the model is on the admin page, it can be changed there without changing the code directly, which is extremely convenient. The admin interface is customizable and although coding is required, many methods and variables are included with Django to achieve customizations with a few lines of code.


Example of database entries

An administration feature does exist for the Google App Engine although it doesn't seem to have the same number of features as Django's feature. Both interfaces give the ability to add, edit and delete entries in the database. Interesting features of the Django admin interface are

  • The ability to customize the look of the interface. The developer designs how the fields and text within are displayed. Tools such as calendars and clocks are built into the interface.
  • A search bar to search through entries in the database. The searching can also be done with filters which are also built into the interface.
  • Histories of administrator activities such as adding, editing and deletion are kept automatically.

Use of Regular Expressions in URL parsing Edit

Django enables the programmer to decouple URLs from their underlying functions by parsing the URL as a string rather than using it as a link. The URL is matched to a string pattern in the URL configuration file using regular expressions and is then matched it to a view. In comparison, in PHP the URL must correspond to the location of the code. In an earlier Python framework (CherryPy), the URL had to correspond to the name of the method used. These coupling rules cannot be maintained when the application reaches a high level of complexity.

Many interesting uses can be made of the Django design.

First, as alluded to above, a view can be swapped, or the URL can be changed very easily in the URL configuration file because of the decoupling.

Second, no "query" parameters are necessary in Django URLs, allowing for intuitive and elegant URLs. A typical html URL will contain ? and & to indicate the locations of parameters being passed in. In Django, the parameter is indicated in the configuration file through a match to a specific regular expression pattern. This makes URLs cleaner by eliminating the & and ? symbols as well as gives URLs logical meanings. For example, if we were making some sort of calculator we could have a URL like that would return the result of 1+5=6. In a way this gives the user a new way of interacting with a webpage; the user can easily perform different addition operations without having to click anywhere in the browser.

Django also easily allows the programmer to handle the case of a user entering invalid data. Let's say we have a car sales site where cars can be found with a URL such as which would bring up the page for the 2007 Accord. Let's say the user enters the URL Limits on the range of matched values can be done with regular expressions. The no URL pattern will be matched and no view will be called. Django gives the ability to do checks on input without using any Python code. Instead, the browser will be taken directly to the default 404 error view.

Third, Django allows multiple URLs to easily match to the same view. With some clever design it would also be possible to match the URL to the same view as Regular expressions allow ranges of characters such as numbers or letters to be matched. 2 separate patterns could be created in this case which would correctly map the appropriate part of the URL string to the correct input parameter to the view.

Strengths Edit

Django is an extremely simple and decoupled framework. Like we talked about in class, designing abstractions is the biggest issue in computer science. Django does an excellent job of abstracting between models and views, and the different components of a web application. Using Django, a programmer can write view functions and then easily map URLs to them using URLConf. Templates can also be used so that the logic of a page is separate from the appearance of a page, allowing different people to change different aspects of the application. The template system does not assume Python knowledge or experience, so that web page designers can easily write templates to change a page's appearance without having to be full fledged programmers, effectively decoupling the system.

Django's template system is also very flexible. Although its template language is very strong, and most people tend to use it, Django also allows the programmer to use a different template language very easily.

Django's optional automatic admin interface is a huge strength, because it reduces the amount of coding on the programmer's behalf. Furthermore, the views and variables can all be adjusted in the admin interface, allowing the admin to manage all the acquired data without dealing with code.

Weaknesses Edit

Because the template system is decoupled from the programming itself, the template cannot change the value of a variable, but can only render the variable's value. The programmer can write custom tags to accomplish this task, but the shipped tags in the system do not allow this.

Similarly, the template system cannot call raw Python code, because it's intended to be decoupled from the actual programming. Custom tags can also be written to do this, but the functionality is not built into Django itself.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.