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.


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…
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

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p