Archive for the PythonDjango Category

(Disclaimer: I was approached by the publisher of the book and I received it at no cost).

This is a short review for the book "Django 1.0 Template Development" by Scott Newman, this book can be found at the publisher’s page or in Amazon for example.

The book was released by Packt Publishing on December 11, 2008 and its ISBN-10/ ISBN-13 are 1847195709 and 978-1847195708. There’s also an electronic (eBook) version available.

In summary this book is a great addition to anyone who has to work with Django’s template system.

The book is aimed at web developers and template authors and although previous knowledge of Django’s templates is not required, the reader should be familiar with the basic workings of Django. The skill level spans from beginner to intermediate, so if you are looking for particulars about an advanced topic I suggest you look at this book’s table of contents to see if it’s covered. I’m not aware of any other book dedicated exclusively to Django’s templates.

This book is pretty updated; the latest version of Django at the time of this writing is 1.1, and it was released recently (July 29, 2009) and according to the release notes there are only a couple of minor improvements that affect the templates and they are all backwards-compatible.

The writing style is clear and easy to follow. Also the visual aspect of the book (page layout, fonts, using warnings and tips boxes etc) is pleasant.

I like that throughout the book they use one particular example or “demo application” and also the code for it can be downloaded from the publisher’s web site.

Regarding the contents, chapters one and two are an introduction to templates and Django’s model, similar to the tutorial in the Django’s web site. It’s good that the basics of how the main elements of Django (models, URLs, views) are reviewed so we don’t have to jump to another reference and we get a refresher of some basic ideas.

Chapter 3 deals with the Template Context. Chapter 4 is a reference for the built-in template tags and filters, ordered alphabetically. This is a long chapter but if you are working a lot with Django’s templates this reference could by itself justify purchasing the book, since here you get explanations and examples while in the Django’s web site documentation you just get a brief description.

In the next three chapters we get some intermediate template usage like inheritance or custom tags and filters. Finally, there are also a few topics that are more advanced and they are explained with examples: pagination, customizing the admin templates, caching and internationalization.

From Wordpress to Django

I made the jump and since last night this blog is powered by Django.

There's no particular reason for the move; Wordpress is a very capable blogging platform (probably the leader at this point) and the only thing that was bothering me was the frequent security warnings where basically you have to keep the software updated to the latest version (although this is pretty straight-forward) and some details like still receiving spam even if I have the comments deactivated.

I've been playing with Python and Django for a while and it seems that a rite of passage for Pythonistas is creating your own web framework, but I set a much humbler goal to do within a few hours: porting the basic Wordpress functionality using Django. Actually is not a "port" but having a basic blog (I don't use special or fancy plug-ins etc, I just want the barebones) reusing Wordpress' database and one of its templates.

Coding with Django has been a breeze; you accomplish a lot with very few lines of code. The Django data model was created from inspection of the WP database, I only had to insert timestamps in a couple of fields where the introspection tool stumbled. I tried to defined foreign keys in the model but it didn't work. In any case I'll be using in the future a new leaner database schema and that will simplify my code (for example the relationship between posts and topics is many-to-many, so besides tables for topics and posts we need another one. WP uses a fourth one) and I'll also be able to use the foreign keys in the code.

Django is great for migrations since you can have its code anywhere and use its url matching to take charge of handling url requests to the old application. For example I have my Wordpress application under a "/wordpress" directory and just by switching on a directive in Apache Django can take over it (I made it a little more complicated by changing the main blog directory name with great imagination to 'blog').

I still need to add some more functionality: more views (archives etc), search, RSS feed and perhaps comments. My prejudice is that the new app will be more secure since the main entry point is now just Django's admin tool.

After having a prototype running with the built-in Django development server I moved to Apache, and here's were I spent most of the time. I had a weird problem where Django under Apache was working (kind of), it will give me its 404 error page if I went to a page not defined in my urls.py but the pages will show up blank.

Finally after narrowing down the problem I get an error: "ImproperlyConfigured: Error importing middleware django.contrib.sessions.middleware MySQLdb-1.2.1p2 or newer is required", this was kind of funny because the development server never complained. I tried setting 'mysql-old' instead of mysql in the settings only to get some other error. So I downloaded mysql-python 1.2.2 and when I tried to install it, I had a complaint about the version of setuptools, so I downloaded the latest version of it as well, and after uninstalling older stuff, setting the egg path in Apache, doing a magic dance to the moon and some fiddling around I got it all working.