<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chicken Scratches &#187; cheetah</title>
	<atom:link href="http://www.chickenwingsw.com/scratches/category/cheetah/feed" rel="self" type="application/rss+xml" />
	<link>http://www.chickenwingsw.com</link>
	<description>Developing ideas on developing.</description>
	<lastBuildDate>Fri, 22 Jul 2011 15:21:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Eddie&#8217;s Django Tip #2: Don&#8217;t Get Trapped</title>
		<link>http://www.chickenwingsw.com/scratches/programming/python/django-tip-2</link>
		<comments>http://www.chickenwingsw.com/scratches/programming/python/django-tip-2#comments</comments>
		<pubDate>Tue, 10 May 2011 13:21:43 +0000</pubDate>
		<dc:creator>Eddie Sullivan</dc:creator>
				<category><![CDATA[cheetah]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.chickenwingsw.com/scratches/programming/eddies-django-tip-2-dont-get-trapped</guid>
		<description><![CDATA[This is the second in my series that I am calling "Eddie's Practices" for Django, in which I present tips and tricks that I have gleaned over the years of working with the Django web application framework. If you haven’t seen Eddie’s Django Tip #1: Choose Names Carefully, why not take a look at it [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second in my series that I am calling <strong>"Eddie's Practices"</strong> for <a href="http://www.djangoproject.com">Django</a>, in which I present tips and tricks that I have gleaned over the years of working with the Django web application framework. If you haven’t seen <a href="http://www.chickenwingsw.com/scratches/programming/python/django-tips-1">Eddie’s Django Tip #1: Choose Names Carefully</a>, why not take a look at it now?</p> <p>Today's tip is:</p> <h2>Don't Get Trapped in the Django Universe</h2> <p><a href="http://www.nasa.gov/mission_pages/hubble/science/hubble-rose-gallery.html"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 0px 10px 10px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="Hubble Rose" border="0" alt="Hubble Rose" align="right" src="http://www.chickenwingsw.com/wp-content/uploads/2011/05/Hubble-Rose.jpg" width="287" height="216"></a>One of Django's strengths is its "batteries included" approach. It provides for free many of the features you would normally have to write from scratch or cobble together from disparate sources. Some examples are the authentication system, the administrative interface, and the templating system. These are wonderfully useful, and they often work together in ways that separately developed libraries can not. However, not every part of Django will fit your needs all the time. Choices were made in the design of Django, and those aren’t always the same choices you would have made.</p> <p>Fortunately, another strength of Django is its modularity. It is very easy to unplug one of its built-in features and plug in your own or someone else's. I'll go through some of the Django features that you may want to consider replacing or eliminating.</p> <p>The important point I want to emphasize is that there is nothing <em>unclean</em> or dangerous about doing this. It's your site, you're the boss of Django, and you should only use the parts of it that you need. I think the creators of Django would agree with me.</p><span id="more-248"></span><h3>The Templating System</h3> <p>There is nothing sacred about the Django templating system, and no reason you <em>have</em> to use it in your application. In fact, the more I use Django templates, the more I appreciate the power and flexibility of <a href="http://cheetahtemplate.org/">Cheetah</a>, and so in most of my recent projects I have used Cheetah for my templates. You do lose out on some of the generic templates and template tags that Django provides, but these are easily replaced, especially because Cheetah allows calling arbitrary Python functions from within a template. As a solo developer, I find this power very convenient.</p> <p>If there is interest, I will be happy to share some of the functions and code I have developed to make it easier to use Cheetah within Django.</p> <h3>The Authentication System</h3> <p>Django makes it very easy to replace or change the built-in authentication/authorization system. You might want to do this if you already have a system in place: for example, if you use HTTP authentication via Apache. Likewise if you want to do something wacky like authentication by IP address within an intranet. Or maybe you have a completely public site with no need for authentication at all.</p> <p>The simplest way to customize your authentication is to provide a <a href="http://docs.djangoproject.com/en/dev/topics/auth/#writing-an-authentication-backend">custom Authentication Backend</a>. But you could go so far as to completely replace or eliminate the <code>django.contrib.auth</code> app. Just be aware that the Django admin site relies on <code>django.contrib.auth</code>.</p> <h3>The Object-Relational Mapping (ORM)</h3> <p>Django's high-level <code>Model</code> and <code>QuerySet</code> interface is great for abstracting away the details of database operations, so it is rare that you need to write raw SQL. So much so, in fact, that you can write a complicated application without even <em>knowing</em> SQL. This is a bad idea, however.</p> <p>Learn SQL, learn what Django is doing behind the scenes with your queries, and don't be afraid to use <a href="http://docs.djangoproject.com/en/1.3/topics/db/sql/">raw SQL</a> when it is appropriate. There are some things you just can't do with Django's ORM: for example, any operation that involves reading and updating a field atomically. Other operations are just much faster if you can do them raw. A good rule of thumb is that if you are performing queries or updates inside a Python loop, investigate switching it to SQL. One big query is generally much faster than a bunch of smaller queries.</p> <h3>The Database Itself</h3> <p>Yes! Despite what you may have heard, it is possible to create a web application without a database! In fact, for a recent site I developed I did just that. I use Django in a very bare-bones way: Django provides URL parsing and dispatching, a logical source code layout, the handling of HTTP requests and responses, and some utility functions, but that's it. Apache takes care of authentication, Cheetah handles my templates, and I store user data in a JSON formatted text file. With no database, there is no need for an admin site - I can edit the text files directly in the file system. There are valid reasons for all these decisions, and for this project they fit my needs perfectly. It's still a "Django-powered" site, but only in a very limited sense.</p> <h3>Django Itself</h3> <p>This should go without saying, but for many new developers it needs to be said. A web application does not need a web framework. Blasphemy, I know. But CGI has been around since 1993. Django was created in 2003, and didn't go public for a while after that. So we had ten good years of dynamic web pages before Django even was a glimmer. If you've never done it before, I recommend making at least one pure CGI (or FastCGI) app, so you can better know and understand what Django is doing behind the scenes. It's not magic or mystical or even that difficult. Just as driving a stick-shift makes you a better driver even on an automatic, knowing the details of web protocols will make you a better web developer.</p> <p>Then when you go back to Django you will appreciate all that it is doing for you, as well as realizing that the Django way is not the only way.</p>]]></content:encoded>
			<wfw:commentRss>http://www.chickenwingsw.com/scratches/programming/python/django-tip-2/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python templates &#8211; Django and Cheetah</title>
		<link>http://www.chickenwingsw.com/scratches/programming/python/python-templates-django-and-cheetah</link>
		<comments>http://www.chickenwingsw.com/scratches/programming/python/python-templates-django-and-cheetah#comments</comments>
		<pubDate>Wed, 28 May 2008 19:14:40 +0000</pubDate>
		<dc:creator>Eddie Sullivan</dc:creator>
				<category><![CDATA[cheetah]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.chickenwingsoftware.com/scratches/?p=16</guid>
		<description><![CDATA[My thoughts on two popular Python-based templating engines.]]></description>
			<content:encoded><![CDATA[    <p>
      When writing web applications, sooner or later (usually sooner),
      everybody is going to need a template
      language. String-interpolation just doesn't cut it. We
      need a way to write something that is <em>almost all</em> text
      (or HTML, or XML, or whatever), but with some dynamic pieces
      thrown in.
    </p>
    <p>
      Since this need is so universal, and the basic requirements are
      so easy to describe, many different groups of people have taken
      it upon themselves to create Yet Another Template Language. As
      developers, we can join the fray and roll our own, or we can
      wade through the myriad options available to us to find the one
      that meets our needs or philosophy. Those who use PHP or ASP
      pretty much have the choice made for them, since the languages
      themselves are glorified template processors. Python
      programmers have a lot more options.
    </p>
    <p>
      Here I'm just going to focus on the two Python templating
      languages I have used in real applications: <a href="http://www.cheetahtemplate.org/">Cheetah</a> and the
      <a href="http://www.djangoproject.com">Django</a> templating engine. (Django, of course, is more than
      just templates, but the template subsystem can be used
      independently.) I use and enjoy both of these, but there are
      significant differences that are worth comparing and
      contrasting, when deciding which to use for your particular
      needs. There are other comparisons out there, including <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=146606">one by
      the Benevolent Dictator for Life</a> himself (though that's a bit
      out of date and inaccurate). When choosing which to use, you
      should read as many opinions as you can, then make the decision
      yourself. Presented here are just my personal thoughts.
    </p>
    <span id="more-16"></span>
    <h4>
      Syntax
    </h4>
    <p>
      Both of these languages are plain-text based, rather than XML
      based or any other format. This means it's easy to use them for
      creating formats that are not XML or HTML, like text or even
      code. It also means it's easy to forget to close a tag or to
      create non-compliant pages, so always be sure to test with
      HTML-Tidy or something like that.
    </p>
    <p>
      The two languages are pretty similar in their syntax, and in my
      humble opinion, they're both pretty ugly.
    </p>
    <p>
      Django uses <span class="code">{{</span> and <span class="code">}}</span> to surround variables to be
      interpolated. These variables can be passed through a "filter" -
      a specially designed Python function - using a vertical bar
      |. One additional parameter can be passed to the filter using a
      colon :. So, for example, to include a floating-point variable,
      expressed with two digits after the decimal point, you would
      write something like: <span class="code">{{ myVar|floatformat:2
      }}</span>. There is a lot of punctuation there, but you do get
      used to it.
    </p>
    <p>
      Cheetah's variable interpretation looks more like how it's done
      in shell-scripting languages, with a dollar
      sign <span class="code">$</span> and optional
      brackets <span class="code">{</span>
      and <span class="code">}</span>, so it looks
      like <span class="code">$myVar</span>
      or <span class="code">${myVar}</span>.
    </p>
    <p>
      They both also allow some logic and flow control. Django has
      "tags" surrounded by {% and %}. Cheetah has "directives" that
      start with # and end with either another # or a newline. Cheetah
      also allows embedding pure Python using
      ASP-like <span class="code">&lt;%</span>
      and <span class="code">%&gt;</span>.
    </p>
    <p>
      Aesthetically, I prefer the dollar signs of Cheetah for
      variables, but prefer Django's approach to the logic
      sections. Either way, you'd better do some finger exercises to
      limber up before typing.
    </p>
    <h4>
      Philosophy
    </h4>
    <p>
      The major philosophical difference between Cheetah and Django
      templating is the amount of programming power  each
      system makes available in the templates themselves. In some
      circles, there is a priority placed on separation of content
      from code, often going so far as to delegate the tasks to
      different people. According to this philosophy, designers should
      work on the presentation, writing HTML and so on, and
      programmers should work on the logic and back-end behavioral
      aspects. In the real world, this is an impossible division,
      because the two are often so closely intertwined, and the very
      existence of template languages is an admission of this fact. A
      template is, by definition, a mixture of logic and content. The
      difference is in the extent of this mixture.
    </p>
    <p>
      Cheetah makes the whole Python interpreter available to the
      template writer. Django provides a simple set of "tags" and
      "filters," allowing limited functionality. These can be extended
      through "custom tags" and "custom filters." According to the
      Django documentation, this limitation is intentional, and is
      meant to limit the mixing of logic and content.
    </p>
    <p>
      In practice, the mixing happens either way. In Cheetah, it is
      easy to end up with a whole lot of Python code in the middle of
      your HTML file, which some would consider inappropriate. In
      Django, it is easy to wind up constructing pieces of HTML in
      your code files and passing them to the template processor as
      parameters. On one side, the Python infects the HTML; on the
      other side, the HTML infects the Python.
    </p>
    <p>
      Personally, I'm a one-man development team, so I have no problem
      with mixing logic and content, either philosophically or
      pragmatically. I find the Cheetah approach leads to less total
      code required, plus I'm a big believer in the "we're all adults
      here" approach to programming, so I prefer the power that
      Cheetah offers. However, I can understand if people want to
      limit what logic is available in templates, if they are meant to
      be edited by designers, marketers, or pointy-haired bosses. In
      that case, Django might make more sense.
    </p>
    <h4>
      What I use
    </h4>
    <p>
      As I said above, I use both. I use the Django templating system
      when I am working within the framework of Django. Django does
      allow you to use any templating system you like, but then you
      lose the power of the built-in generic views and context
      processors. I use Cheetah when I don't need all the power of the
      Django framework (the database access layer, URL dispatcher,
      authentication, etc.).
    </p>
    <p>
      I have a slight preference for Cheetah, but they're both very
      useful. I hope this post provides some help for those choosing
      between the two.
    </p>
]]></content:encoded>
			<wfw:commentRss>http://www.chickenwingsw.com/scratches/programming/python/python-templates-django-and-cheetah/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

