Skip to main content

Python: Django Templates

"As simple as possible, but no simpler."

I have a lot of respect for what the Django guys have done. I actually really like a lot of Django. However, having tried it, I find the templating engine to be elegant, but too simple. What frustrates me most is the lack of functions. In my first programming class, I learned how to use functions for code reuse. DRY applies even to HTML. At my company, even the "template authors" know how to create a function to avoid code duplication. Nor is the "include" or "block" statement a suitable replacement--functions have parameters. Different arguments lead to different results. This is necessary functionality. Also, functions should support recursion. I learned in my second programming class how to think recursively, and every once in a while it really comes in handy (for instance, when you're writing a bulletin board application with a hierarchy of messages).

When I was using Django templates, I really felt like my hands were tied. I don't think lack of functions improves security. I don't think custom tags are always a suitable replacement--I'm talking about simple HTML reuse. Please forgive my boldness, but I don't think I should have to live without functions just because a template writer working at some other company doesn't understand them.

Next, I think I'll try out using Django with Cheetah. So far, Cheetah has always met my needs exceedingly well. Hopefully, I can get it to fit in smoothly, maintaining Django's overall elegant feel.

Comments

Anonymous said…
There is no requirement that you use the Django templates, though they are the "official" way that show up in all the docs (which is a good thing).

You might want to try using Myghty with Django... It looks like Myghty gives you the most freedom to define those functions in those templates like you want to. I say "looks like", though, because I haven't used, just skimmed the docs... If Myghty is "just a python library" like Cheetah is, then the integration of Django and Myghty might be pretty easy. So check out Myghty and let me know if I'm way wrong on this.
jjinux said…
Jason. I agree with you, although I'll probably use Cheetah instead. I *like* Myghty, but I've been using Cheetah for two years now. We have well over 100,000 lines of Cheetah company-wide, and I really like it.
Anonymous said…
Actually you can do that stuff quite easily with Django - just produce some custom tags based on the inclusion tags stuff (like it is used in the admin). What this gives you essentially are template-based functions - only that they are written as tags.

Django does have a quite flexible template language - you only need to accept that to do more complex work, you either precompute stuff in the view function or build custom template tags.
Anonymous said…
I agree with you Jason. I understand that the Django templates are meant to be simple so designers can understand them. But just the other day the designer I work with came to me because he wanted to use and OR with the ifequal tag. Django doesn't support this, so I submitted a patch. The resulting feedback let me know that this was not wanted.

I thought about switching to Cheetah for a while, but I enjoy Django and I am still doing things the Django way.

However I have started to create my own library of custom tags that are more robust than the one Django offers.
lalo said…
I felt the same on my first contacts with those templates. But then I had an epiphany. Actually trying to develop with Django made me move all my logic out of the templates and into python (into the view), which is something I have been wanting to do for years.

So for your board problem, I recommend you try that approach rather than changing the template engine; rather than using render_to_response, do the calling and recursing in python, and make calls into the template engine to turn your data into html snippets.
jjinux said…
Quoted:
I felt the same on my first contacts with those templates. But then I had an epiphany. Actually trying to develop with Django made me move all my logic out of the templates and into python (into the view), which is something I have been wanting to do for years.

I hear what you're saying, but I truly believe that even template authors should not be exempt from DRY.
Anonymous said…
I don't think that's what he's saying... using a loop inside the view isn't repeating anything. You're just using the templating engine in a slightly different way. Instead of preparing all of your data, then making a single template call, make smaller template calls as you need them and string the whole thing together with a final render.

Popular posts from this blog

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python . Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening. The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for B

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/ I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive: Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from