<?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>muhuk.com &#187; reusable</title>
	<atom:link href="http://www.muhuk.com/tag/reusable/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.muhuk.com</link>
	<description>know thyself</description>
	<lastBuildDate>Thu, 29 Dec 2011 05:05:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>My Idea Of The Django Blogging App™</title>
		<link>http://www.muhuk.com/2010/04/my-idea-of-the-django-blogging-app%e2%84%a2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=my-idea-of-the-django-blogging-app%25e2%2584%25a2</link>
		<comments>http://www.muhuk.com/2010/04/my-idea-of-the-django-blogging-app%e2%84%a2/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 09:36:48 +0000</pubDate>
		<dc:creator>Atamert Ölçgen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[blogs]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[reusable]]></category>

		<guid isPermaLink="false">http://www.muhuk.com/?p=370</guid>
		<description><![CDATA[I am not going to talk about yet another Django-based blogging engine in this post. There are a number of blogging apps which try to be like turn-key solutions, like a WordPress blog. I have skimmed through the code of many such apps, but haven&#8217;t used one yet. Some of them are really high quality [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2010/04/my-idea-of-the-django-blogging-app%e2%84%a2/' addthis:title='My Idea Of The Django Blogging App™ '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></description>
			<content:encoded><![CDATA[<p>I am not going to talk about yet another Django-based blogging engine in this post. There are a number of blogging apps which try to be like turn-key solutions, like a WordPress blog. I have skimmed through the code of many such apps, but haven&#8217;t used one yet. Some of them are really high quality apps. What I have in mind is somewhat different though. I would like an app that would allow me to build a blog that satisfies my projects specific requirements.</p>

<p>Let me reiterate the last sentence. Having a Django-based blog just because Django is fashinable is a little dumb in my opinion. If Django-based X blogging engine suits you better than anything else, use it. Why not? But my personal choice of blogging engine is WordPress<sup>1</sup>. The value of a Django blogging app, for me, is in <em>adding</em> a blog to a Django project. And different projects might have different requirements. So my idea of a Django blogging app is one that is highly configurable and highly extendable.</p>

<p>On the other hand I don&#8217;t need the convenience of clicking a checkbox on a polished UI. I can write a function. Or I don&#8217;t necessarily need it to, say, provide a navigation menu. There are apps that do that. Even if there wasn&#8217;t it shouldn&#8217;t be the blog app&#8217;s job. So I am not looking for an instant-blog. I have a Django app in my mind, nothing more.</p>

<h3>What Should Be Left Out</h3>

<p>Basically any feature that can be provided by another <a href="http://www.muhuk.com/tag/reusable/">reusable app</a> should be left out. Why should we re-implement something that is already done&#8230; and reviewed by others&#8230; and tested. Of course this doesn&#8217;t necessarily mean providing no convenience functions.</p>

<ul>
<li>No admin. Because we already have <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/">one</a>.</li>
<li>No theming. For the love of Flying Spaghetti Monster, you don&#8217;t need any <em>theming</em> other than what <code>django.template</code> offers. Pre-built themes are for turn-key solutions.</li>
<li>No comments or contact forms. (See <a href="http://docs.djangoproject.com/en/dev/ref/contrib/comments/"><code>django.contrib.comments</code></a> and <a href="http://bitbucket.org/ubernostrum/django-contact-form/"><code>django-contact-form</code></a>)</li>
<li>No <em>official</em> markup format (or formats). This can be handled in the templates without difficulty. But, maybe, pluggable content filters is a good idea. I haven&#8217;t made up my mind on this one entirely. It won&#8217;t use any markup format by default, that is for sure.</li>
</ul>

<h3>What Should Be Included</h3>

<p>Remember, every project has a different set of needed features for its blog. Some need catagories, some need tags and some others need both. But it would end up as a disaster if we implemented each one of those features into <a href="http://www.muhuk.com/2010/01/developing-reusable-django-apps/">a single app</a>. Instead I think it should consist of many small apps that work together. But I wouldn&#8217;t want to end up having huge spaghetti of apps that all depend on one another, like <a href="http://pinaxproject.com/">Pinax</a> does. A minimal amount of core apps<sup>2</sup> and then everything else should be optional. By optional I mean you don&#8217;t have to install packages you won&#8217;t need.</p>

<p>I think the components (apps) should be activated via adding to <code>INSTALLED_APPS</code> and configured with settings. I can&#8217;t think of any parameter that needs to be changed dynamically, so why not use the established way of doing configuration in Django.</p>

<p>Two must have features for such a blogging app are previews and scheduled publishing. It is possible that you sometimes write a post quickly and publish it immediately. But I suppose nobody will say they don&#8217;t care about these two features.</p>

<p>Built-in feeds and sitemaps are also nice to have.</p>

<p>Multiple instances of this blogging app running on the same project? À la <code>admin</code>. I can&#8217;t make my mind on this one. Sure it would be a nice feature. But it could complicate the code. Peehaps too much for a not so common case.</p>

<p>What do you think about the general idea? Are there any other <em>must-have</em> features? Would you be willing to learn a new app when you are already comfortable with another blogging app?</p>

<hr />

<p><strong>1:</strong> Even though it&#8217;s written in the abomination called PHP. But since there are plugins for everything I don&#8217;t have to touch the code.</p>

<p><strong>2:</strong> One sounds like a good number, if possible.</p>
<div><a class="addthis_button" href="http://www.muhuk.com//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2010/04/my-idea-of-the-django-blogging-app%e2%84%a2/' addthis:title='My Idea Of The Django Blogging App™ '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div><p>Related posts:<ol>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django/' rel='bookmark' title='Working with files in Django &#8211; Part 1'>Working with files in Django &#8211; Part 1</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-2/' rel='bookmark' title='Working with files in Django &#8211; Part 2'>Working with files in Django &#8211; Part 2</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-3/' rel='bookmark' title='Working with files in Django &#8211; Part 3'>Working with files in Django &#8211; Part 3</a></li>
</ol></p> <p><a href="http://www.muhuk.com/?flattrss_redirect&amp;id=370&amp;md5=83329d2e3f16f84bd0e5b86894bd6017" title="Flattr" target="_blank"><img src="http://www.muhuk.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.muhuk.com/2010/04/my-idea-of-the-django-blogging-app%e2%84%a2/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<atom:link rel="payment" href="http://www.muhuk.com/?flattrss_redirect&amp;id=370&amp;md5=83329d2e3f16f84bd0e5b86894bd6017" type="text/html" />
	</item>
		<item>
		<title>Developing Reusable Django Apps: Signals</title>
		<link>http://www.muhuk.com/2010/03/developing-reusable-django-apps-signals/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=developing-reusable-django-apps-signals</link>
		<comments>http://www.muhuk.com/2010/03/developing-reusable-django-apps-signals/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 08:49:17 +0000</pubDate>
		<dc:creator>Atamert Ölçgen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[reusable]]></category>
		<category><![CDATA[signals]]></category>

		<guid isPermaLink="false">http://www.muhuk.com/?p=346</guid>
		<description><![CDATA[I wrote &#8220;signals provide a great way to propagate the events generated from your app&#8221; earlier. I think reusable apps should avoid hardcoding any kind of event handling and send signals instead. App consumers might prefer an email over an on-screen notification. They may even choose to ignore the event silently. A reusable app should [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2010/03/developing-reusable-django-apps-signals/' addthis:title='Developing Reusable Django Apps: Signals '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></description>
			<content:encoded><![CDATA[<p>I wrote &#8220;<a href="http://www.muhuk.com/2010/01/developing-reusable-django-apps/"><em>signals provide a great way to propagate the events generated from your app</em></a>&#8221; earlier. I think reusable apps should avoid hardcoding any kind of event handling and <a href="http://docs.djangoproject.com/en/dev/topics/signals/">send signals</a> instead. App consumers might prefer an email over an on-screen notification. They may even choose to ignore the event silently. A reusable app should give this choice to the consumer.</p>

<p>Taking advantage of signals doesn&#8217;t necessarily mean providing no sane defaults. You can send signals <strong>and</strong> provide default event handling. Here is a couple of ideas how this can be done:</p>

<ul>
<li>Your app can <a href="http://stackoverflow.com/questions/2209159/disconnect-signals-for-models-and-reconnect-in-django/2209804#2209804">check if there are any listeners</a> and connect the default handlers if there is none.</li>
<li>You can ship an auxiliary app that connects default handlers when added to <code>INSTALLED_APPS</code>.</li>
</ul>

<p>I personally prefer the second approach since it&#8217;s simpler and more explicit. I&#8217;m sure there are other ways to implement default handlers for signals.</p>

<h3>Dispatch_uid</h3>

<p>Don&#8217;t forget to assign a unique <code>dispatch_uid</code> for each <code>connect()</code> call. Otherwise your handler can get <a href="http://code.djangoproject.com/wiki/Signals#Helppost_saveseemstobeemittedtwiceforeachsave">connected twice</a>. I would also suggest you to use both module path <strong>and</strong> your handler function&#8217;s name in your <code>dispatch_uid</code>:</p>

<pre><code>"%s.%s" % (os.path.splitext(__file__)[0].replace(os.sep, '.')[1:],
           handler_name)
</code></pre>

<p>Now I should take my own advice and replace hardcoded <code>User.message_set.create()</code>s with signals in <a href="http://github.com/muhuk/django-simple-friends">django-simple-friends</a>.</p>
<div><a class="addthis_button" href="http://www.muhuk.com//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2010/03/developing-reusable-django-apps-signals/' addthis:title='Developing Reusable Django Apps: Signals '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div><p>Related posts:<ol>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django/' rel='bookmark' title='Working with files in Django &#8211; Part 1'>Working with files in Django &#8211; Part 1</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-2/' rel='bookmark' title='Working with files in Django &#8211; Part 2'>Working with files in Django &#8211; Part 2</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-3/' rel='bookmark' title='Working with files in Django &#8211; Part 3'>Working with files in Django &#8211; Part 3</a></li>
</ol></p> <p><a href="http://www.muhuk.com/?flattrss_redirect&amp;id=346&amp;md5=aa54d067afb2b49cafc62fa354d6b44b" title="Flattr" target="_blank"><img src="http://www.muhuk.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.muhuk.com/2010/03/developing-reusable-django-apps-signals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.muhuk.com/?flattrss_redirect&amp;id=346&amp;md5=aa54d067afb2b49cafc62fa354d6b44b" type="text/html" />
	</item>
		<item>
		<title>Developing Reusable Django Apps: App Settings</title>
		<link>http://www.muhuk.com/2010/01/developing-reusable-django-apps-app-settings/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=developing-reusable-django-apps-app-settings</link>
		<comments>http://www.muhuk.com/2010/01/developing-reusable-django-apps-app-settings/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 10:09:27 +0000</pubDate>
		<dc:creator>Atamert Ölçgen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[reusable]]></category>
		<category><![CDATA[settings]]></category>

		<guid isPermaLink="false">http://www.muhuk.com/?p=332</guid>
		<description><![CDATA[Eventually your app will need some sort of configuration. Supplying many parameters to customise your views, template tags and filters to allow template authors easily harness the power of your app and implementing registration pattern are all sensible things to do. But at some point you will need configuration for your app at project level. [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2010/01/developing-reusable-django-apps-app-settings/' addthis:title='Developing Reusable Django Apps: App Settings '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></description>
			<content:encoded><![CDATA[<p>Eventually your app will need some sort of configuration. Supplying many parameters to customise your views, template tags and filters to allow template authors easily harness the power of your app and implementing registration pattern are all sensible things to do. But at some point you will need configuration for your app at project level. Because app level configuration is <del>stupid</del><a href="http://www.youtube.com/watch?v=A-S0tqpPga4">not reusable</a>. Consumers of your app should never have to change its source code. And what better place to put our configuration statements than <code>settings.py</code>? Remember; we want to make things easier for our app&#8217;s consumers, not harder. There is no need to add a new file to the project for a few lines of settings<sup>1</sup>.</p>

<p>We <a href="http://docs.djangoproject.com/en/dev/topics/settings/#using-settings-in-python-code">already know</a> we shouldn&#8217;t import <code>settings.py</code> directly. Instead we import the <code>settings</code> object (much like a singleton) of <a href="http://code.djangoproject.com/browser/django/trunk/django/conf/__init__.py"><code>django.conf</code></a> module:</p>

<pre><code>from django.conf import settings
</code></pre>

<p>Now you can access different configuration options as attributes on this object. But I suggest you to use <a href="http://docs.python.org/library/functions.html#getattr"><code>getattr()</code></a> in order to avoid getting <code>AttributeError</code>s. Also, notice how we <em>didn&#8217;t</em> hardcode the name of the attribute in the second method below:</p>

<pre><code># This is too verbose
try:
    some_setting = settings.SOME_SETTING
except AttributeError:
    some_setting = DEFAULT_VALUE

# Plain and simple
some_setting = getattr(settings, 'SOME_SETTING', DEFAULT_VALUE)
</code></pre>

<p>Instead of requiring consumers to define all your app settings it is better to supply sensible defaults. Also I find it useful to prefix names of app settings within <code>settings.py</code>.</p>

<pre><code># in settings.py
MYAPP_FOO_CHOICES = [('bar', u'Bar'), ('baz', u'Baz')]


# in myapp/models.py
from django.db import models
from django.conf import settings


FOO_CHOICES = getattr(settings, 'MYAPP_FOO_CHOICES', [('quux', u'Quux')])


class FooRecord(models.Model):
    foo = models.CharField(max_length=10, choices=FOO_CHOICES)
</code></pre>

<p>This works fine for simple apps with fewer settings. But it can easily get out of hand when your app grows. An <code>app_settings.py</code> module would help keeping track of configuration by keeping all configuration options in one place:</p>

<pre><code># in myapp/app_settings.py
from django.conf import settings


FOO_CHOICES = getattr(settings, 'MYAPP_FOO_CHOICES', [('quux', u'Quux')])


# in myapp/models.py
from django.db import models
from app_settings import FOO_CHOICES


class FooRecord(models.Model):
    foo = models.CharField(max_length=10, choices=FOO_CHOICES)
</code></pre>

<p>To summarize the points above:</p>

<ul>
<li>Import <code>settings</code> from <code>django.conf</code></li>
<li>Use <code>getattr()</code></li>
<li>Always supply a default value.</li>
<li>Prefix settings you made up in <code>settings.py</code></li>
<li>Use <code>app_settings.py</code> if you have many</li>
</ul>

<hr />

<p><strong>1</strong>: If your configuration is long, say more than 100 lines, you should step back and reconsider. Perhaps you should prefer a strategy similar to <code>django.contrib.sitemaps</code> or <code>django.contrib.syndication</code>.</p>
<div><a class="addthis_button" href="http://www.muhuk.com//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2010/01/developing-reusable-django-apps-app-settings/' addthis:title='Developing Reusable Django Apps: App Settings '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div><p>Related posts:<ol>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django/' rel='bookmark' title='Working with files in Django &#8211; Part 1'>Working with files in Django &#8211; Part 1</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-2/' rel='bookmark' title='Working with files in Django &#8211; Part 2'>Working with files in Django &#8211; Part 2</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-3/' rel='bookmark' title='Working with files in Django &#8211; Part 3'>Working with files in Django &#8211; Part 3</a></li>
</ol></p> <p><a href="http://www.muhuk.com/?flattrss_redirect&amp;id=332&amp;md5=917218710790c82c11ae3685a006afea" title="Flattr" target="_blank"><img src="http://www.muhuk.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.muhuk.com/2010/01/developing-reusable-django-apps-app-settings/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="http://www.muhuk.com/?flattrss_redirect&amp;id=332&amp;md5=917218710790c82c11ae3685a006afea" type="text/html" />
	</item>
		<item>
		<title>Developing Reusable Django Apps</title>
		<link>http://www.muhuk.com/2010/01/developing-reusable-django-apps/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=developing-reusable-django-apps</link>
		<comments>http://www.muhuk.com/2010/01/developing-reusable-django-apps/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 15:06:40 +0000</pubDate>
		<dc:creator>Atamert Ölçgen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[reusable]]></category>

		<guid isPermaLink="false">http://www.muhuk.com/?p=320</guid>
		<description><![CDATA[Django app structure is an implementation of seperation of concerns. It is slightly different than what you can find in other MVC frameworks. The stack is split vertically, not horizontally. And then the app is split horizontally within, i.e. models, views, templates etc are in their seperate modules/packages/directories. This vertical splitting allows you to collect [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2010/01/developing-reusable-django-apps/' addthis:title='Developing Reusable Django Apps '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.muhuk.com/2009/03/django-where-should-my-app-live/">Django app structure</a> is an implementation of <a href="http://en.wikipedia.org/wiki/Separation_of_concerns">seperation of concerns</a>. It is slightly different than what you can find in other MVC frameworks. The stack is split vertically, not horizontally. And then the app is split horizontally within, i.e. models, views, templates etc are in their seperate modules/packages/directories. This vertical splitting allows you to collect all ingredients of one functionality in your <a href="http://www.b-list.org/weblog/2006/sep/10/django-tips-laying-out-application/">project</a> in one place.</p>

<p><a href="http://www.muhuk.com/wp-content/uploads/2010/01/django__developing_reusable_apps_1.png"><img src="http://www.muhuk.com/wp-content/uploads/2010/01/django__developing_reusable_apps_1.png" alt="Framework Structure" title="Framework Structure" width="580" height="350" class="size-full wp-image-318" /></a></p>

<p>I think apps<sup>1</sup> are one of the strong points of Django. A selling point if you like. There is a great ecosystem of apps, you can find an app for almost anything posssible with Python. And Python is kick-ass when it comes to library wealth. But there is another major advantage of apps when they&#8217;re done right; a sane code base. Here is a slide from the <a href="http://jacobian.org/speaking/oscon-2009/oscon/">Django in the Real World</a> presentation by Jacob Kaplan-Moss:</p>

<blockquote>
  <p>The fivefold path</p>
  
  <ul>
  <li>Do one thing, and do it well.</li>
  <li>Don’t be afraid of multiple apps.</li>
  <li>Write for ﬂexibility.</li>
  <li>Build to distribute.</li>
  <li>Extend carefully.</li>
  </ul>
</blockquote>

<p>I will focus on flexibility and interoperability of apps in this post. But before we proceed I would like to emphasize the first bullet point in the slide. Because the scope of your app plays a big role in its flexibility and interoperability. Apps should be small enough to easily understand and integrate (into a project). Many times I have moved away from an otherwise good app because of its many dependencies and/or excessive features. On the other hand apps should be big enough to allow for different configurations and allow extension without modifying their code. <em>Do one thing, and do it well</em>.</p>

<p><a href="http://www.muhuk.com/wp-content/uploads/2010/01/django__developing_reusable_apps_2.png"><img src="http://www.muhuk.com/wp-content/uploads/2010/01/django__developing_reusable_apps_2.png" alt="Scope of an app" title="Scope of an app" width="580" height="200" class="size-full wp-image-319" /></a></p>

<p>Take <a href="http://code.google.com/p/django-tagging/">django-tagging</a> for example; it&#8217;s 1.3 KLOC but it does tagging and nothing else. There are no dependencies other than Django, you can add tags to any model without modifying the model source, a tag can be associated with any type of model and <code>tagging</code> hides the gory details from you&#8230; In short; finding the right size is important. This is why <code>tagging</code> is <strong>the</strong> tagging app for Django.</p>

<h2>Building For Reuse</h2>

<p>General advice is &#8220;<em>even project specific apps should be reusable</em>&#8220;. Slapping the same app onto another project is not the only advantage. In fact it may not be possible if you are not in the habit of upgrading your whole project to recent versions of Django. The main advantage as I have said before is <em>sanity</em>. I prefer Django to other web frameworks/environments because it provides a civilized way of development. Let&#8217;s accept it; web programming is not a particularly interesting, exciting or intellectually rewarding field. You write the same piece of code over and over. And worst of all the challanges you face are actually a result of either the underlying system was designed by morons or you are trying to use it for something it&#8217;s not intended to be used. So it is only natural that web programmers feel they&#8217;re rusting. Django eases the pain. If you stick to certain conventions serenity will follow as well.</p>

<p>Naturally the framework does most of the work regarding app flexibility and interoperability. Take URLs for instance <code>include('myapp.urls')</code> and you are good to go. You don&#8217;t have to bind views one by one. Is it inflexible? Who said <code>urls.py</code> can only contain a hardcoded list of URLs. You can do anything that is possible with Python. You can generate different <code>urlpatterns</code> based on a setting for instance.</p>

<p>It is relatively easy and straightforward to reuse and extend forms and views (both function based and class based). Models are a little harder to get right though. You should always think of the most difficult situation which is you can&#8217;t touch either app&#8217;s code. Registration pattern of <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-objects"><code>admin</code></a> app provides a good solution here. You can <code>register</code> a third party model to another third party app in just a few lines.</p>

<p>You don&#8217;t need to write lots of code to get the flexibility and interoperability. Well designed apps make good use of <code>settings.py</code> for example. Why should the project developer wrap a view when a single line assignment would do the job? Supplying good templatetags and template snipplets (includes) is another way to make things easy for app consumers.</p>

<p>Signals provide a great way to propagate the events generated from your app. Even though they are one way<sup>2</sup>, signals are extremely powerful. Any number of observers can connect to a signal and you can send a signal anywhere in your code. Literally. It is even possible your app suppying a signal and then another app sending it<sup>3</sup>.</p>

<p>There are many more ways to tame your app to be reusable. It all starts with your determination and discipline. Just like documentation, testing and maintaining a software generally. I will write more about reusable apps.</p>

<hr />

<p><strong>1</strong>: The word <strong>application</strong> is used both for a <strong>web application</strong> and a <strong>Django application</strong>. To avoid confusion I always use <strong>app</strong> to indicate the latter.</p>

<p><strong>2</strong>: Signals don&#8217;t have return values. But you can use a callback AFAIK.</p>

<p><strong>3</strong>: I can&#8217;t think of an example this would be useful, but still&#8230;</p>
<div><a class="addthis_button" href="http://www.muhuk.com//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2010/01/developing-reusable-django-apps/' addthis:title='Developing Reusable Django Apps '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div><p>Related posts:<ol>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django/' rel='bookmark' title='Working with files in Django &#8211; Part 1'>Working with files in Django &#8211; Part 1</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-2/' rel='bookmark' title='Working with files in Django &#8211; Part 2'>Working with files in Django &#8211; Part 2</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-3/' rel='bookmark' title='Working with files in Django &#8211; Part 3'>Working with files in Django &#8211; Part 3</a></li>
</ol></p> <p><a href="http://www.muhuk.com/?flattrss_redirect&amp;id=320&amp;md5=5addb855b526305831fed4d2dcad8764" title="Flattr" target="_blank"><img src="http://www.muhuk.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.muhuk.com/2010/01/developing-reusable-django-apps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.muhuk.com/?flattrss_redirect&amp;id=320&amp;md5=5addb855b526305831fed4d2dcad8764" type="text/html" />
	</item>
		<item>
		<title>Django: Where Should My App Live</title>
		<link>http://www.muhuk.com/2009/03/django-where-should-my-app-live/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=django-where-should-my-app-live</link>
		<comments>http://www.muhuk.com/2009/03/django-where-should-my-app-live/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 11:00:44 +0000</pubDate>
		<dc:creator>Atamert Ölçgen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[reusable]]></category>

		<guid isPermaLink="false">http://www.muhuk.com/?p=153</guid>
		<description><![CDATA[Python is my preferred language and Django is my preferred environment for web applications. Django&#8217;s probably the most important strength is how it splits different parts of an application and allows (and encourages) decoupling between them. This way you get to have many small re-usable and, if designed well, highly configurable apps (in Django terms). [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2009/03/django-where-should-my-app-live/' addthis:title='Django: Where Should My App Live '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></description>
			<content:encoded><![CDATA[<p><a href="http://python.org/">Python</a> is my preferred language and <a href="http://djangoproject.com/">Django</a> is my preferred environment for web applications. Django&#8217;s probably the most important strength is how it splits different parts of an application and allows (and encourages) decoupling between them. This way you get to have many small re-usable and, if designed well, highly configurable <strong>apps</strong> (in Django terms).</p>

<p>The frameworks itself comes with a set of apps, <strong>contrib apps</strong>. These are officially supported applications for most general tasks. For example Django has a <code>syndication</code> app that allows you to create feeds for your content easily, a <code>comments</code> app for.. well comments, a <code>redirects</code> app for dynamically managing URL redirects. And let&#8217;s not forget the renowned <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#ref-contrib-admin"><code>admin</code></a> app.</p>

<p><strong>Third party re-usable apps</strong> can be used to complement contrib apps. These apps provide more specific funtionality, such as <a href="http://github.com/jezdez/django-robots/">providing a dynamically generated robots.txt</a>. Third party apps usually have no dependencies other than Django (and therefore contrib apps). Best third party apps are the ones that provide just enough functionality on one task. It&#8217;s the similarity with <a href="http://en.wikipedia.org/wiki/Unix_philosophy#Mike_Gancarz:_The_UNIX_Philosophy">UNIX philosophy</a> that makes Django apps so attractive; do one thing, do it well.</p>

<p>Finally there is your project specific code. Although it is not a must, I think it is a good design pattern to put project specific code in one or more <strong>project specific apps</strong>. These apps will probably have lots of dependencies to other apps, but that is cool. These apps will only function as glue between re-usable apps.</p>

<h3>Django Default</h3>

<p>How does <code>django-admin</code> (or <code>manage.py</code> within your project) handle apps? &#8220;<code>manage.py startapp appname</code>&#8221; command creates a new app inside your project directory with the following file structure:</p>

<pre><code>appname/
    __init__.py
    models.py
    views.py
</code></pre>

<p>And then, assuming your project directory name is <code>myproject</code>, you can import your models like &#8220;<code>from myproject.appname.models import Foo</code>&#8220;. Two things to note here:</p>

<ul>
<li>Your new app is inside your project directory. This is normal, but significant at the same time. More on this later.</li>
<li>Your code is coupled with the project directory&#8217;s name. You might be fine with it in the beginning, but at some point you should realize that the project you are working on is not a library, it is an application. Therefore it ought to be independent of the containing folder name. But the real problem is that <em>your app is coupled with the project, tightly</em>.</li>
</ul>

<p>The first point is not an obstacle until you decide to distribute your app individually. An app is just a library, it can live anywhere inside your PYTHONPATH. In other words; as long as you can import it, location of your app is irrelevant.</p>

<p>The second point is important. If you plan to re-use your apps, you need to:</p>

<ol>
<li>Make sure you have cleaned up all project specific code.</li>
<li>Minimize dependencies to other apps, especially non-contrib apps.</li>
</ol>

<h3>Adding Project Directory To PYTHONPATH</h3>

<p>A quick fix could be to add the project directory to PYTHONPATH. This makes all libraries within accessible from python.</p>

<p>You don&#8217;t need to modify PYTHONPATH for <a href="http://docs.djangoproject.com/en/dev/ref/django-admin/#runserver-optional-port-number-or-ipaddr-port">development server</a>. But for both development and production environments you will need to modify your <code>settings.py</code> with the following:</p>

<pre><code><span style="color:#ff3030; font-weight:bold">import</span> os

<span style="color:#38ad24"># Absolute path for the project directory</span>
_PATH <span style="color:#555555">=</span> os<span style="color:#555555">.</span>path<span style="color:#555555">.</span><span style="color:#d11ced">abspath</span><span style="color:#555555">(</span>os<span style="color:#555555">.</span>path<span style="color:#555555">.</span><span style="color:#d11ced">dirname</span><span style="color:#555555">(</span>__file__<span style="color:#555555">))</span>

<span style="color:#38ad24"># Name of the project directory</span>
_MODULE <span style="color:#555555">=</span> os<span style="color:#555555">.</span>path<span style="color:#555555">.</span><span style="color:#d11ced">basename</span><span style="color:#555555">(</span>_PATH<span style="color:#555555">)</span>


<span style="color:#38ad24"># Define absolute path for media</span>
MEDIA_ROOT <span style="color:#555555">=</span> _PATH <span style="color:#555555">+</span> <span style="color:#1861a7">'/media/'</span>

<span style="color:#38ad24"># Decouple project directory</span>
ROOT_URLCONF <span style="color:#555555">=</span> _MODULE <span style="color:#555555">+</span> <span style="color:#1861a7">'.urls'</span>

<span style="color:#38ad24"># Import apps within porject directory</span>
<span style="color:#38ad24"># independent from its name</span>
INSTALLED_APPS <span style="color:#555555">= (</span>
    <span style="color:#1861a7">'django.contrib.auth'</span><span style="color:#555555">,</span>
    <span style="color:#1861a7">'django.contrib.contenttypes'</span><span style="color:#555555">,</span>
    <span style="color:#1861a7">'django.contrib.sessions'</span><span style="color:#555555">,</span>
    <span style="color:#1861a7">'django.contrib.sites'</span><span style="color:#555555">,</span>
    _MODULE <span style="color:#555555">+</span> <span style="color:#1861a7">'.myapp'</span><span style="color:#555555">,</span>
    _MODULE <span style="color:#555555">+</span> <span style="color:#1861a7">'.yourapp'</span><span style="color:#555555">,</span>
<span style="color:#555555">)</span></code></pre>

<p>This only makes project directory an independent variable, adding a bit of flexibility. But your apps are still within the project directory and from a practical point of view; they are not yet ready to be shared between projects.</p>

<h3>Seperating Apps From Projects</h3>

<p>As I said above, and app is a library. If you can import it, it can live anywhere. I personally prefer putting all my re-usable apps in one directory and symlinking them somewhere within <a href="http://docs.python.org/tutorial/modules.html#the-module-search-path">PYTHONPATH</a>.</p>

<pre><code><span style="color:#38ad24"># Import apps directly</span>
INSTALLED_APPS <span style="color:#555555">= (</span>
    <span style="color:#1861a7">'django.contrib.auth'</span><span style="color:#555555">,</span>
    <span style="color:#1861a7">'django.contrib.contenttypes'</span><span style="color:#555555">,</span>
    <span style="color:#1861a7">'django.contrib.sessions'</span><span style="color:#555555">,</span>
    <span style="color:#1861a7">'django.contrib.sites'</span><span style="color:#555555">,</span>
    <span style="color:#1861a7">'myapp'</span><span style="color:#555555">,</span>
    <span style="color:#1861a7">'yourapp'</span><span style="color:#555555">,</span>
<span style="color:#555555">)</span>
</code></pre>

<p>Now if your app is decoupled in code level it should be re-usable since we have decoupled it from the project directory. This, however easy at the beginning, introduces a little more work in terms of maintenance. You need to deploy and maintain your re-usable apps and projects seperately.</p>

<h3>Lifetime Of An App</h3>

<p>I always do my best to model any app as re-usable. But to speed up my first few iterations I follow the plan below:</p>

<ol>
<li>First thing I do when I create a new project is to set up <code>_MODULE</code> &amp; <code>_PATH</code> in my <code>settings.py</code>.</li>
<li>Initially I allow my apps to be tightly coupled. Adding customizability and extensibility where it is <em>easy</em>.</li>
<li>Once it functions the way I want, I start de-coupling. If it is truly project-specific code, I just spend minimal effort de-coupling. I don&#8217;t think this is a waste of time. It will probably pay off in the future as less maintenance cost.</li>
<li>When the app has <del>no</del> minimal dependencies, I move it to my django apps dir. So far I have had the advantage of using the same (source control) repository with the project. But now the app is mature and it should live in its own repository.</li>
</ol>
<div><a class="addthis_button" href="http://www.muhuk.com//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2009/03/django-where-should-my-app-live/' addthis:title='Django: Where Should My App Live '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div><p>Related posts:<ol>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django/' rel='bookmark' title='Working with files in Django &#8211; Part 1'>Working with files in Django &#8211; Part 1</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-2/' rel='bookmark' title='Working with files in Django &#8211; Part 2'>Working with files in Django &#8211; Part 2</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-3/' rel='bookmark' title='Working with files in Django &#8211; Part 3'>Working with files in Django &#8211; Part 3</a></li>
</ol></p> <p><a href="http://www.muhuk.com/?flattrss_redirect&amp;id=153&amp;md5=d8059f2662067ba39d67fe10066b6c18" title="Flattr" target="_blank"><img src="http://www.muhuk.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.muhuk.com/2009/03/django-where-should-my-app-live/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="http://www.muhuk.com/?flattrss_redirect&amp;id=153&amp;md5=d8059f2662067ba39d67fe10066b6c18" type="text/html" />
	</item>
	</channel>
</rss>

