<?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"
	>

<channel>
	<title>ChipLog &#187; Djblets</title>
	<atom:link href="http://www.chipx86.com/blog/tag/djblets/feed/rss2" rel="self" type="application/rss+xml" />
	<link>http://www.chipx86.com/blog</link>
	<description>Virtualization, Open Source, and Life</description>
	<pubDate>Tue, 27 May 2008 10:07:38 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Django Development with Djblets: Unrooting your URLs</title>
		<link>http://www.chipx86.com/blog/?p=253</link>
		<comments>http://www.chipx86.com/blog/?p=253#comments</comments>
		<pubDate>Fri, 04 Apr 2008 06:35:11 +0000</pubDate>
		<dc:creator>ChipX86</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Django]]></category>

		<category><![CDATA[Djblets]]></category>

		<category><![CDATA[Review-Board]]></category>

		<guid isPermaLink="false">http://www.chipx86.com/blog/?p=253</guid>
		<description><![CDATA[Typically, Django sites are designed with the assumption that they&#8217;ll have a domain or subdomain to themselves. Often times this is fine, but if you&#8217;re developing a web application designed for redistribution, sometimes you can&#8217;t make that assumption.
During development of Review Board, many of our users wanted the ability to install Review Board into a [...]]]></description>
			<content:encoded><![CDATA[<div class='series_toc'><h3>Table of contents for Django Development with Djblets</h3><ol><li><a href='http://www.chipx86.com/blog/?p=244' title='Django Development with Djblets'>Django Development with Djblets</a></li><li><a href='http://www.chipx86.com/blog/?p=245' title='Django Development with Djblets: Custom Tag Helpers'>Django Development with Djblets: Custom Tag Helpers</a></li><li>Django Development with Djblets: Unrooting your URLs</li></ol></div> <p>Typically, Django sites are designed with the assumption that they&#8217;ll have a domain or subdomain to themselves. Often times this is fine, but if you&#8217;re developing a web application designed for redistribution, sometimes you can&#8217;t make that assumption.</p>
<p>During development of <a href="http://www.review-board.org/">Review Board</a>, many of our users wanted the ability to install Review Board into a subdirectory of one of their domains, rather than a subdomain.</p>
<p>There&#8217;s a few rules that are important when making your site relocatable:</p>
<ul>
<li>Always use <tt>MEDIA_URL</tt> when referring to your media directory, so that people can customize where they put their media files.</li>
<li>Don&#8217;t hard-code URLs in templates. Use the <tt>{% url %}</tt> tag or <tt>get_absolute_url()</tt> when possible.</li>
</ul>
<p>These solve some of the issues, but doesn&#8217;t address relocating a Django site to a subdirectory.</p>
<p>Djblets fills in this gap by providing a special URL handler designed to act as a prefix for all your projects&#8217; URLs. To make use of this, you need to modify your <tt>settings.py</tt> file as follows:</p>
<p><b>settings.py</b></p>

<div class="wp_syntax"><div class="code"><pre class="python">TEMPLATE_CONTEXT_PROCESSORS = <span style="color: black;">&#40;</span>
    ...
    <span style="color: #483d8b;">'djblets.util.context_processors.siteRoot'</span>,
<span style="color: black;">&#41;</span>
&nbsp;
SITE_ROOT_URLCONF = <span style="color: #483d8b;">'yourproject.urls'</span>
ROOT_URLCONF = <span style="color: #483d8b;">'djblets.util.rooturl'</span>
&nbsp;
SITE_ROOT = <span style="color: #483d8b;">'/'</span></pre></div></div>

<p><tt>SITE_ROOT</tt> specifies where the site is located. By default, this should be &#8220;/&#8221;, but this can be changed to point to any path. For example, &#8220;/myproject/&#8221;. Note that it should always have a leading and a trailing slash.</p>
<p>The custom template context processor (<tt>djblets.util.context_processors.siteRoot</tt>) will make <tt>SITE_ROOT</tt> accessible to templates.</p>
<p><tt>SITE_ROOT</tt> should be used in templates when you need to refer to URLs that aren&#8217;t designed to respect <tt>SITE_ROOT</tt> (such as <tt>User.get_absolute_url</tt>). Your own custom applications should always respect <tt>SITE_ROOT</tt> whenever providing a URL.</p>
<p><tt>ROOT_URLCONF</tt> is typically what you would set to point to your project&#8217;s URL. However, in this case, you&#8217;ll be pointing it to <tt>djblets.util.rooturl</tt>. This in turn will forward all URLs to your project&#8217;s handler, defined in <tt>SITE_ROOT_URLCONF</tt>.</p>
<p>This is all you need to have a fully relocatable Django site!</p>
<p>To sum up:</p>
<ol>
<li>Add <tt>djblets.util.context_processors.siteRoot</tt> to your <tt>TEMPLATE_CONTEXT_PROCESSORS</tt>.</li>
<li>Set <tt>SITE_ROOT_URLCONF</tt> to your project&#8217;s URL handler.</li>
<li>Set <tt>ROOT_URLCONF</tt> to &#8216;djblets.util.rooturl&#8217;</li>
<li>Prefix any URLs with <tt>SITE_ROOT</tt> in your templates, unless the URL would already take <tt>SITE_ROOT</tt> into account.</li>
</ol>
<p>This is functionality that will hopefully make its way into Django at some point. For now, you have an easy way of unrooting your Django project.</p>
 <div class='series_links'><a href='http://www.chipx86.com/blog/?p=245' title='Django Development with Djblets: Custom Tag Helpers'>Previous in series</a> </div>]]></content:encoded>
			<wfw:commentRss>http://www.chipx86.com/blog/?feed=rss2&amp;p=253</wfw:commentRss>
		</item>
		<item>
		<title>Django Development with Djblets: Custom Tag Helpers</title>
		<link>http://www.chipx86.com/blog/?p=245</link>
		<comments>http://www.chipx86.com/blog/?p=245#comments</comments>
		<pubDate>Fri, 29 Feb 2008 12:37:46 +0000</pubDate>
		<dc:creator>ChipX86</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Django]]></category>

		<category><![CDATA[Djblets]]></category>

		<category><![CDATA[Review-Board]]></category>

		<guid isPermaLink="false">http://www.chipx86.com/blog/?p=245</guid>
		<description><![CDATA[I&#8217;m planning to cover all of what Django can do, but for now, let&#8217;s start simple with something most Django developers spend way too much time creating: Custom tags.
Django&#8217;s nice enough to provide a @register.simple_tag decorator for creating very basic tags that don&#8217;t take a context but do take parameters. This is great, but what [...]]]></description>
			<content:encoded><![CDATA[<div class='series_toc'><h3>Table of contents for Django Development with Djblets</h3><ol><li><a href='http://www.chipx86.com/blog/?p=244' title='Django Development with Djblets'>Django Development with Djblets</a></li><li>Django Development with Djblets: Custom Tag Helpers</li><li><a href='http://www.chipx86.com/blog/?p=253' title='Django Development with Djblets: Unrooting your URLs'>Django Development with Djblets: Unrooting your URLs</a></li></ol></div> <p>I&#8217;m planning to cover all of what Django can do, but for now, let&#8217;s start simple with something most Django developers spend way too much time creating: Custom tags.</p>
<p>Django&#8217;s nice enough to provide a <tt>@register.simple_tag</tt> decorator for creating very basic tags that don&#8217;t take a context but do take parameters. This is great, but what if you want more? Many Django applications use the same boilerplate time and time again to create their tags, but we make it much easier.</p>
<p>Introducing <tt>@basictag</tt> and <tt>@blocktag</tt>.</p>
<p><b><tt>@basictag</tt></b></p>
<p>Ever wanted to use Django&#8217;s <tt>@register.simple_tag</tt> but needed access to the context? I&#8217;ve found far too many cases where this would be useful, but Django doesn&#8217;t make this easy. Your tag code would end up looking like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">class</span> MyTagNode<span style="color: black;">&#40;</span>template.<span style="color: black;">Node</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, arg1, arg2<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">arg1</span> = arg1
        <span style="color: #008000;">self</span>.<span style="color: black;">arg2</span> = arg2
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> render<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, context<span style="color: black;">&#41;</span>:
        arg1 = Variable<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">arg1</span><span style="color: black;">&#41;</span>.<span style="color: black;">resolve</span><span style="color: black;">&#40;</span>context<span style="color: black;">&#41;</span>
        arg2 = Variable<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">arg2</span><span style="color: black;">&#41;</span>.<span style="color: black;">resolve</span><span style="color: black;">&#40;</span>context<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> context<span style="color: black;">&#91;</span><span style="color: #483d8b;">'user'</span><span style="color: black;">&#93;</span>
&nbsp;
@register.<span style="color: black;">tag</span>
<span style="color: #ff7700;font-weight:bold;">def</span> mytag<span style="color: black;">&#40;</span><span style="color: #dc143c;">parser</span>, <span style="color: #dc143c;">token</span><span style="color: black;">&#41;</span>:
    bits = <span style="color: #dc143c;">token</span>.<span style="color: black;">split_contents</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> MyTagNode<span style="color: black;">&#40;</span>bits<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, bits<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Do this a few times and it&#8217;s bound to drive you nuts. How about this instead?</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> djblets.<span style="color: black;">util</span>.<span style="color: black;">decorators</span> <span style="color: #ff7700;font-weight:bold;">import</span> basictag
&nbsp;
@register.<span style="color: black;">tag</span>
@basictag<span style="color: black;">&#40;</span>takes_context=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> mytag<span style="color: black;">&#40;</span>context, arg1, arg2<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> context<span style="color: black;">&#91;</span><span style="color: #483d8b;">'user'</span><span style="color: black;">&#93;</span></pre></div></div>

<p>Far less code and increased readability. Hooray!</p>
<p><b><tt>@blocktag</tt></b></p>
<p><tt>@blocktag</tt> aims to do the same thing @basictag does but for block tags. A block tag is a tag that contains nested content, like <tt>@spaceless</tt> or <tt>@for</tt>. This usually requires even more boilerplate than the above code fragment, except with the added complexity of having to grab the contents of the block.</p>
<p>We&#8217;ve condensed it down to this:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> djblets.<span style="color: black;">util</span>.<span style="color: black;">decorators</span> <span style="color: #ff7700;font-weight:bold;">import</span> blocktag
&nbsp;
@register.<span style="color: black;">tag</span>
@blocktag
<span style="color: #ff7700;font-weight:bold;">def</span> blinkblock<span style="color: black;">&#40;</span>context, nodelist, arg1, arg2<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&amp;lt;blink&amp;gt;%s&amp;lt;/blink&amp;gt;&quot;</span> % nodelist.<span style="color: black;">render</span><span style="color: black;">&#40;</span>context<span style="color: black;">&#41;</span></pre></div></div>

<p>If you&#8217;ve built block tags in the past, you&#8217;ll appreciate how simple that was.</p>
 <div class='series_links'><a href='http://www.chipx86.com/blog/?p=244' title='Django Development with Djblets'>Previous in series</a> <a href='http://www.chipx86.com/blog/?p=253' title='Django Development with Djblets: Unrooting your URLs'>Next in series</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.chipx86.com/blog/?feed=rss2&amp;p=245</wfw:commentRss>
		</item>
		<item>
		<title>Django Development with Djblets</title>
		<link>http://www.chipx86.com/blog/?p=244</link>
		<comments>http://www.chipx86.com/blog/?p=244#comments</comments>
		<pubDate>Fri, 29 Feb 2008 12:35:35 +0000</pubDate>
		<dc:creator>ChipX86</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Django]]></category>

		<category><![CDATA[Djblets]]></category>

		<category><![CDATA[Review-Board]]></category>

		<guid isPermaLink="false">http://www.chipx86.com/blog/?p=244</guid>
		<description><![CDATA[Django is an awesome development platform for web applications. With such features as database abstraction, template/view/url separation, built-in authentication with interchangeable backends, it&#8217;s made web development much more enjoyable.
We use Django in Review Board with much success. Over time, as we&#8217;ve come to develop new features, we realized that much of our codebase was useful [...]]]></description>
			<content:encoded><![CDATA[<div class='series_toc'><h3>Table of contents for Django Development with Djblets</h3><ol><li>Django Development with Djblets</li><li><a href='http://www.chipx86.com/blog/?p=245' title='Django Development with Djblets: Custom Tag Helpers'>Django Development with Djblets: Custom Tag Helpers</a></li><li><a href='http://www.chipx86.com/blog/?p=253' title='Django Development with Djblets: Unrooting your URLs'>Django Development with Djblets: Unrooting your URLs</a></li></ol></div> <p><a href="http://www.djangoproject.com/">Django</a> is an awesome development platform for web applications. With such features as database abstraction, template/view/url separation, built-in authentication with interchangeable backends, it&#8217;s made web development much more enjoyable.</p>
<p>We use Django in <a href="http://www.review-board.org/">Review Board</a> with much success. Over time, as we&#8217;ve come to develop new features, we realized that much of our codebase was useful outside of Review Board and, bit by bit, moved pieces into a library we call Djblets.</p>
<p><b>What does Djblets do?</b></p>
<p>A bit of everything, really. Any time we have useful functionality that isn&#8217;t tied to Review Board, we put it here.</p>
<p>Djblet&#8217;s feature list currently consists of:</p>
<ul>
<li>Authentication improvements, making it easy to register and login in one step, seamlessly, handle password recovery, and more.</li>
<li>Flexible datagrids for displaying data in a paginated list with user-specific column customization, ordering and sorting.</li>
<li>Decorators to drastically simplify creation of simple and block template tags.</li>
<li>Caching functions for calling a function and caching the result if the data isn&#8217;t already in the cache, and a special URL pattern matcher that prevents caching of any contained URLs.</li>
<li>Unit testing utility classes.</li>
</ul>
<p>And of course more little things here and there.</p>
<p><b>Downloading Djblets</b></p>
<p>Djblets is not a released app, but it&#8217;s pretty stable and well tested. You can check out a copy from our <a href="http://svn.navi.cx/misc/trunk/djblets/djblets">SVN repository</a>, or automatically include it in your own repository through an <tt>svn:externals</tt> entry.</p>
<p>Djblets is licensed under the MIT license, making it usable in most projects.</p>
<p><b>Using Djblets</b></p>
<p>Over time I&#8217;ll be writing articles on using the many features of Djblets. See the other posts in the series, or dig around the Djblets source code.</p>
 <div class='series_links'> <a href='http://www.chipx86.com/blog/?p=245' title='Django Development with Djblets: Custom Tag Helpers'>Next in series</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.chipx86.com/blog/?feed=rss2&amp;p=244</wfw:commentRss>
		</item>
	</channel>
</rss>
