Django & Python Blog

Understanding Django Templates and Their Goals

In the real world, such as in metalworking, a template is a mold used to shape or pattern a material. In computing, typically in the context of a word processor, a template is a file that comes with a preset format and preset content, which the user then fills in with more content.
Django templates take elements from both real-world templates and word-processing templates. A Django template is an external file or document that acts as a guide or blueprint for data, while also providing preset content. These blueprints, written by the developer, allow Django to quickly generate markup content to be returned to the user.

An easy way to think about templates is to liken them to fill-in-the-blank exercises. Each template provides content and structure with intentional blanks. We carefully identify each blank, giving it a purpose or some meaning. We then tell Django to assemble webpages using data and a template, making use of the purpose or meaning assigned to each blank in the template to properly fill it in with data.

If we return to our metallurgy analogy for (a very loose) comparison, the template is the mold and the data are the metal we are pouring. However, the mold here provides not just structure but also elements of the final item we are creating, and the metal we are pouring can be selectively poured or ignored.

Templates are thus external to Django: they are a document Django fills in. In fact, they are the only part of the framework that is not in Python. Templates are markup-language agnostic: a developer may output HTML, JSON, YAML, or any other imaginable text format. This is because the developer may write the template in any format and simply have Django fill in the blanks the developer leaves and indicates.

An important implication of templates’ external nature is that Django has a method for importing the data from the document into Django, allowing Django to then identify the gaps the developer has left and fill them in. The Django template loader is an important piece of the puzzle.


Understanding Django Migrations

Models allow us to define the structure, relations, and behavior of the data we use on our website. The behavior we’ve defined will have a direct effect on our use of the data in Python (as we shall see in the next section), but the structure we’ve created is just an abstraction for how the data will be stored in the database. In Django, models and the database schema are reflections of one other. Any modification of one must result in the modification of the other. In a fully deployed team project, this can be quite tricky. If one developer makes a change to his or her local test database, he or she needs a way to share these changes with other developers and the various servers running the website(s) in a repeatable and version-controlled manner. If the change turns out to be in error, the developer should also have an easy way to undo his or her changes. Finally, if two developers are working on the same area of the code and make conflicting changes, it should be easy to recognize the issue and resolve the problem.

Migrations solve these problems by providing a controlled, predictable system for altering a database. The typical workflow with Django is to 1. Create or change a model in Django, 2. Generate a migration file, 3. Use the migration file to create/alter the database.
The migration file contains the instructions to alter the database and to roll back (or revert) those alterations. The advantage of keeping these instructions in a file is that this file may be easily run (applied) and shared among developers. Migration files may be considered version control for the database, although the analogy is imperfect. The advantage of having these changes as files (and where the analogy breaks down) is the ability to store and share these files using a standard version-control system.

Migrations are a key part of every Django project, as they help avert major headaches and save time.

jQuery and Ajax Integration in Django

We will be working with the leading Python web framework, Django, on the server side, and jQuery-powered Ajax on the client side. During the course of this post, we will cover the basic technologies and then see them come together in an employee intranet photo directory that shares some Web 2.0 strengths.

There is more than one good JavaScript library; we will be working with jQuery, which has reached acceptance as a standard lightweight JavaScript library. It might be suggested that Pythonistas may and much to like in jQuery: jQuery, like Python, was carefully designed to enable the developer to get powerful results easily.

Human speech: An overlaid function

Human speech is an overlaid function. What is meant by this is rejected in the answer to a question: “What part of the human body has the basic job of speech?” The tongue, for one answer, is used in speech, but it also tastes food and helps us swallow. The lungs and diaphragm, for another answer, perform the essential task of breathing. The brain cannot be overlooked, but it also does a great many other jobs. All of these parts of the body do something more essential than speech and, for that matter, all of these can be found among animals that cannot talk. Speech
is something that is overlaid over organs that are there in the first place because of something other than speech.
Something similar to this is true for Ajax, which is not a technology in itself,
but something overlaid on top of other technologies. Ajax, some people say,
stands for Asynchronous JavaScript and XML, but that was a retroactive expansion. JavaScript was introduced almost a decade before people began seriously talking about Ajax. Not only is it technically possible to use Ajax without JavaScript (one can substitute VBScript at the expense of browser compatibility), but there are
quite a few substantial reasons to use JavaScript Object Notation (JSON) in lieu of heavy-on-the-wire eXtensible Markup Language (XML). Performing the overlaid function of Ajax with JSON replacing XML is just as eligible to be considered full- edged Ajax as a solution incorporating XML.

Ajax: Another overlaid function

Ajax is a way of using client-side technologies to talk with a server and perform partial page updates. Updates may be to all or part of the page, or simply to data handled behind the scenes. It is an alternative to the older paradigm of having a whole page replaced by a new page loaded when someone clicks on a link or submits a form. Partial
page updates, in Ajax, are associated with Web 2.0, while whole page updates are associated with Web 1.0; it is important to note that “Web 2.0” and “Ajax” are not interchangeable. Web 2.0 includes more decentralized control and contributions besides Ajax, and for some objectives it may make perfect sense to develop an e-commerce site that uses Ajax but does not open the door to the same kind of community contributions as Web 2.0.