<?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>The Code Dreamer : Nitin Hayaran &#187; Django</title>
	<atom:link href="http://www.nitinh.com/category/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nitinh.com</link>
	<description>Nitin Hayaran&#039;s Blog</description>
	<lastBuildDate>Sun, 29 Jan 2012 07:28:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Django Template Tag to protect the E-mail address</title>
		<link>http://www.nitinh.com/2010/02/django-template-tag-to-protect-the-e-mail-address/</link>
		<comments>http://www.nitinh.com/2010/02/django-template-tag-to-protect-the-e-mail-address/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 10:26:51 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[bots]]></category>
		<category><![CDATA[Javasript]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[spam]]></category>
		<category><![CDATA[spamming]]></category>
		<category><![CDATA[spiders]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[template-tag]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=431</guid>
		<description><![CDATA[TweetShareHere is a Django Template tag which you can use to protect the E-mail address on your website against bots or spiders that index or harvest E-mail addresses. It uses a substitution cipher with a different key for every page load. This template tag encrypts the email address and generates a equivalent Javascript code that [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/' rel='bookmark' title='Permanent Link: Implementing Search in Django Site using HayStack and Xapian / Whoosh'>Implementing Search in Django Site using HayStack and Xapian / Whoosh</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-via="socializeWP" data-url="http://www.nitinh.com/2010/02/django-template-tag-to-protect-the-e-mail-address/">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="socialize-in-button-left"><a name="fb_share" type="box_count" share_url="http://www.nitinh.com/2010/02/django-template-tag-to-protect-the-e-mail-address/" href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div></div><p><img class="alignleft" style="margin: 0 10px 10px 0;" src="http://imgur.com/qAuL2.jpg" alt="E-mail Security" width="150" height="86" />Here is a Django Template tag which you can use to protect the E-mail address on your website against bots or spiders that index or harvest E-mail addresses. It uses a substitution cipher with a different key for every page load.<span id="more-431"></span></p>
<p>This template tag encrypts the email address and generates a equivalent Javascript code that can decrypt it. This Javascript produce an email link when it runs on browser. As most bots and spider don't executes Javascript they wouldn't be able to extract mail addresses. While a visitor of your web page will not notice that you used this script as long as he/she has javascript enabled. </p>
<p>A normal visitor with a javascript enabled browser will find no difference.  The visitor with non-javascript browser will see “[javascript protected email address]” in stead of the E-mail address.</p>
<h3>Template Tag Code</h3>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> EncryptEmail<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>, context_var<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">context_var</span> = template.<span style="color: black;">Variable</span><span style="color: black;">&#40;</span>context_var<span style="color: black;">&#41;</span><span style="color: #808080; font-style: italic;"># context_var</span>
    <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>:
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
        email_address = <span style="color: #008000;">self</span>.<span style="color: black;">context_var</span>.<span style="color: black;">resolve</span><span style="color: black;">&#40;</span>context<span style="color: black;">&#41;</span>
        character_set = <span style="color: #483d8b;">'+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'</span>
        char_list = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>character_set<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">random</span>.<span style="color: black;">shuffle</span><span style="color: black;">&#40;</span>char_list<span style="color: black;">&#41;</span>
&nbsp;
        key = <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>char_list<span style="color: black;">&#41;</span>
&nbsp;
        cipher_text = <span style="color: #483d8b;">''</span>
        <span style="color: #008000;">id</span> = <span style="color: #483d8b;">'e'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">random</span>.<span style="color: black;">randrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">999999999</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> email_address:
            cipher_text += key<span style="color: black;">&#91;</span> character_set.<span style="color: black;">find</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span> <span style="color: black;">&#93;</span>
&nbsp;
        script = <span style="color: #483d8b;">'var a=&quot;'</span>+key+<span style="color: #483d8b;">'&quot;;var b=a.split(&quot;&quot;).sort().join(&quot;&quot;);var c=&quot;'</span>+cipher_text+<span style="color: #483d8b;">'&quot;;var d=&quot;&quot;;'</span>
        script += <span style="color: #483d8b;">'for(var e=0;e&lt;c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));'</span>
        script += <span style="color: #483d8b;">'document.getElementById(&quot;'</span>+<span style="color: #008000;">id</span>+<span style="color: #483d8b;">'&quot;).innerHTML=&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\\</span>&quot;mailto:&quot;+d+&quot;<span style="color: #000099; font-weight: bold;">\\</span>&quot;&gt;&quot;+d+&quot;&lt;/a&gt;&quot;'</span>
&nbsp;
&nbsp;
        script = <span style="color: #483d8b;">&quot;eval(<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>+ script.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span>,<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&quot;'</span>,<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\\</span>&quot;'</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span>
        script = <span style="color: #483d8b;">'&lt;script type=&quot;text/javascript&quot;&gt;/*&lt;![CDATA[*/'</span>+script+<span style="color: #483d8b;">'/*]]&gt;*/&lt;/script&gt;'</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'&lt;span id=&quot;'</span>+ <span style="color: #008000;">id</span> + <span style="color: #483d8b;">'&quot;&gt;[javascript protected email address]&lt;/span&gt;'</span>+ script
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span>  encrypt_email<span style="color: black;">&#40;</span><span style="color: #dc143c;">parser</span>, <span style="color: #dc143c;">token</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
        {% encrypt_email user.email %}
    &quot;&quot;&quot;</span>
&nbsp;
    tokens = <span style="color: #dc143c;">token</span>.<span style="color: black;">contents</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>tokens<span style="color: black;">&#41;</span><span style="color: #66cc66;">!</span>=<span style="color: #ff4500;">2</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> template.<span style="color: black;">TemplateSyntaxError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%r tag accept two argument&quot;</span> <span style="color: #66cc66;">%</span> tokens<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> EncryptEmail<span style="color: black;">&#40;</span>tokens<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
register.<span style="color: black;">tag</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'encrypt_email'</span>, encrypt_email<span style="color: black;">&#41;</span></pre></div></div>

<h3>Usage</h3>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span><span style="color: #66cc66;">%</span> encrypt_email <span style="color: #dc143c;">user</span>.<span style="color: #dc143c;">email</span> <span style="color: #66cc66;">%</span><span style="color: black;">&#125;</span></pre></div></div>

<p>The javascript code it generates is something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;e857606962&quot;</span>&gt;</span>[javascript protected email address]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
/*<span style="color: #009900;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>*<span style="color: #66cc66;">/</span>eval<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;var a=\&quot;</span>cnel8gMuoCjaYRDkv7p+ErHI5qL2x-O3Q@GdtbWKUwT9VfZSN_J04.P1hiFAXz6Bmys\<span style="color: #ff0000;">&quot;;var b=a.split(\&quot;</span>\<span style="color: #ff0000;">&quot;).sort().join(\&quot;</span>\<span style="color: #ff0000;">&quot;);var c=\&quot;</span>AXzVfPXCRgMAhw9fAe91.\<span style="color: #ff0000;">&quot;;var d=\&quot;</span>\<span style="color: #ff0000;">&quot;;for(var e=0;e&lt;c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));document.getElementById(\&quot;</span>e857606962\<span style="color: #ff0000;">&quot;).innerHTML=\&quot;</span>&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span>\\\<span style="color: #ff0000;">&quot;mailto:\&quot;</span>+d+\<span style="color: #ff0000;">&quot;\\\&quot;</span>&gt;</span>\&quot;+d+\&quot;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>\&quot;&quot;)/*]]&gt;*/
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<h4>Demo</h4>
<p>For demo visit : </p>
<p><a class='button' href='http://apps.nitinh.com/encrypt-email/' target="_blank">Demo</a></p>
<p>Hope this template tag will help you to secure your e-mail addresses from bots and spider. </p>
<p>Lets have a spam free web.</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/' rel='bookmark' title='Permanent Link: Implementing Search in Django Site using HayStack and Xapian / Whoosh'>Implementing Search in Django Site using HayStack and Xapian / Whoosh</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2010/02/django-template-tag-to-protect-the-e-mail-address/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Implementing Search in Django Site using HayStack and Xapian / Whoosh</title>
		<link>http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/</link>
		<comments>http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 12:08:34 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Daniel]]></category>
		<category><![CDATA[Daniel Lindsley]]></category>
		<category><![CDATA[HayStack]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Whoosh]]></category>
		<category><![CDATA[Xapian]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=275</guid>
		<description><![CDATA[TweetShare For one of my personal project OnlyAds.in , I wanted to implement search. The requirements I had were : Easy &#038; Fast to Install / Setup Can perform reasonably complex searches But until we have anything like django.contrib.search, we'll have to work with alternate applications. The options we have are : django haystack search [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/02/django-example-filefield-and-imagefield/' rel='bookmark' title='Permanent Link: Django Example: FileField and ImageField'>Django Example: FileField and ImageField</a></li>
<li><a href='http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/' rel='bookmark' title='Permanent Link: Debugging Django on Windows : Developement Setup'>Debugging Django on Windows : Developement Setup</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-via="socializeWP" data-url="http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="socialize-in-button-left"><a name="fb_share" type="box_count" share_url="http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/" href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div></div><p><img class="alignleft" style="margin: 0 10px 10px 0;" title="Python Django" src="http://www.bildirgec.org/imaj/ceyranci/python-django.png" alt="" width="148" height="134" /> For one of my personal project <strong><a href="http://www.onlyads.in">OnlyAds.in</a></strong> , I wanted to implement search. The requirements I had were :</p>
<ul>
<li>Easy &#038; Fast to Install / Setup</li>
<li>Can perform reasonably complex searches</li>
</ul>
<p>But until we have anything like <code>django.contrib.search</code>, we'll have to work with alternate applications.<span id="more-275"></span></p>
<p>The options we have are :
<ul>
<li><a href="http://haystacksearch.org/"><b>django haystack search</b></a></li>
<li><a rel="nofollow" href="http://github.com/dcramer/django-sphinx">django-sphinx</a></li>
<li><a rel="nofollow" href="http://code.google.com/p/djapian/">http://code.google.com/p/djapian/</a></li>
<li><a rel="nofollow" href="http://code.google.com/p/django-search-lucene/">http://code.google.com/p/django-search-lucene/</a></li>
<li><a rel="nofollow" href="http://www.sai.msu.su/~megera/postgres/gist/tsearch/V2/">http://www.sai.msu.su/~megera/postgres/gist/tsearch/V2/</a></li>
</ul>
<p>For this particular project I went ahead with <code>HayStack Search</code>. The decision for choosing HayStack was great, the implementation was very easy, and the performance of search is also fair enough.</p>
<h3>Implementation</h3>
<p>In this section i'll guide you through implementation procedure of haystack using Xapian / Whoosh.<br />
For this tutorial i'll be using my <strong>Video Model</strong> which is in <strong>myApp</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Video<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
	title = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span>
	desc = models.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	thumbnail = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span>
	tags = TagField<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	creation_date = models.<span style="color: black;">DateTimeField</span><span style="color: black;">&#40;</span>auto_now_add=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></div></div>

<p>HayStack basically provide option of choosing search backend from the following options:</p>
<ul>
<li><a href="http://whoosh.ca/">Whoosh</a></li>
<li><a href="http://xapian.org/download">Xapian</a></li>
<li><a href="http://www.apache.org/dyn/closer.cgi/lucene/solr/">Solr</a></li>
</ul>
<p>You need to choose one of the search backend and install it in your system. I would recommend Xapian for its simplicity and ease to setup. However if you find it difficult to install, you can go ahead with Whoosh. For install instructions please visit : <a target="_blank" href="http://haystacksearch.org/docs/installing_search_engines.html">Installing Search Engines</a></p>
<p>After Installing search backend you need to install <a target="_blank" href="http://github.com/toastdriven/django-haystack/tree/master">haystack</a></p>
<p>Now you are ready to go ahead with actual implementation.</p>
<h4>Step 1</h4>
<p>In <code>settings.py</code>, add <code>haystack</code> to <code>INSTALLED_APPS</code>.</p>
<h4>Step 2</h4>
<p>Create a file <code>search_sites.py</code> in your project directory. In my case the project name was <code>myproject</code>, so I created myproject/search_sites.py. The content of search_sites.py are like this.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> haystack
haystack.<span style="color: black;">autodiscover</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h4>Step 3</h4>
<p>Enter SearchBackend info in <code>settings.py</code></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">HAYSTACK_SITECONF = <span style="color: #483d8b;">'myproject.search_sites'</span>
<span style="color: #808080; font-style: italic;">#If you choose whoosh as search backend uncomment following 2 lines and comment last 2 lines</span>
<span style="color: #808080; font-style: italic;">#HAYSTACK_SEARCH_ENGINE = 'whoosh'</span>
<span style="color: #808080; font-style: italic;">#HAYSTACK_WHOOSH_PATH = '/home/xyz/django_projects/index'</span>
HAYSTACK_SEARCH_ENGINE = <span style="color: #483d8b;">'xapian'</span>
HAYSTACK_XAPIAN_PATH = <span style="color: #483d8b;">'/home/xyz/django_projects/xapian-index'</span></pre></div></div>

<p>You'll need to create respective directories. The search backend will store index files in these directories.</p>
<h4>Step 4</h4>
<p>Now you'll have to create a SearchIndexes file for all the apps where you want search. For e.g in my case I need to search <code>Video</code> of <strong>myApp</strong>, so i'll create a <code>search_indexes.py</code> file in <code>myApp</code>.<br />
The file is like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>
<span style="color: #ff7700;font-weight:bold;">from</span> haystack <span style="color: #ff7700;font-weight:bold;">import</span> indexes
<span style="color: #ff7700;font-weight:bold;">from</span> haystack <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">site</span>
<span style="color: #ff7700;font-weight:bold;">from</span> myproject.<span style="color: black;">ads</span>.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> Video
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> VideoIndex<span style="color: black;">&#40;</span>indexes.<span style="color: black;">SearchIndex</span><span style="color: black;">&#41;</span>:
    text = indexes.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>use_template=<span style="color: #008000;">True</span>, document=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    title = indexes.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>model_attr=<span style="color: #483d8b;">'title'</span><span style="color: black;">&#41;</span>
    desc = indexes.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>model_attr=<span style="color: #483d8b;">'desc'</span><span style="color: black;">&#41;</span>
    tags = indexes.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>model_attr=<span style="color: #483d8b;">'tags'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_queryset<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Used when the entire index for model is updated.&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> Video.<span style="color: black;">objects</span>.<span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>creation_date__lte=<span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">site</span>.<span style="color: black;">register</span><span style="color: black;">&#40;</span>Video, VideoIndex<span style="color: black;">&#41;</span></pre></div></div>

<h4>Step 5</h4>
<p>Create a template for the field for which we have kept <code>use_template=True</code> i.e., text (see the code above, with first field "text"). The path of this template should be <code>search/indexes/{app_name}/{modelClass}_{fieldname}.txt</code>.<br />
In my case the file was in <code>templates/search/indexes/myApp/video_text.txt</code>. The content of this file is like this.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> <span style="color: #008000;">object</span>.<span style="color: black;">title</span> <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> <span style="color: #008000;">object</span>.<span style="color: black;">desc</span> <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span><span style="color: black;">&#123;</span> <span style="color: #008000;">object</span>.<span style="color: black;">tags</span> <span style="color: black;">&#125;</span><span style="color: black;">&#125;</span></pre></div></div>

<h4>Step 6</h4>
<p>Run <code>manage.py reindex</code><br />
This will index the objects, and index files will be created in the directories you mentioned in settings.py</p>
<h4>Step 7</h4>
<p>In this step you need to actually write a view and template to search your content. An example of it is given below</p>
<h5>myApp/views.py</h5>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> search_videos<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">try</span>:
		sTerm = request.<span style="color: black;">GET</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'q'</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">except</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> HttpResponseRedirect<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span>
&nbsp;
	results = SearchQuerySet<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">auto_query</span><span style="color: black;">&#40;</span>sTerm<span style="color: black;">&#41;</span>
	videos = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> r <span style="color: #ff7700;font-weight:bold;">in</span> results:
		videos.<span style="color: black;">append</span><span style="color: black;">&#40;</span>r.<span style="color: #008000;">object</span><span style="color: black;">&#41;</span>
	<span style="color: #808080; font-style: italic;">#videos list contains all the videos those match the search criteria</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span>videos<span style="color: black;">&#41;</span></pre></div></div>

<h4>Hurrry !!! Thats all</h4>
<p>This is all we need to do for implementing search. The <code>auto_query(sTerm)</code> function is very impressive as it also support complex search queries like:<br />
india "cute kid" -young</p>
<h4>Long Live Django Community</h4>
<p>I was really impressed by HayStack. One reason for this was its power / ease of use, but the bigger reason was <strong><a href="http://djangopeople.net/daniellindsley/">Daniel Lindsley</a></strong> [<a href="http://www.toastdriven.com/about/"><strong>1</strong></a>]. He is the creator of HayStack. He is very helpful person, always available in IRC Chat for help developer like me.</p>
<p>Thanks Daniel.</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/02/django-example-filefield-and-imagefield/' rel='bookmark' title='Permanent Link: Django Example: FileField and ImageField'>Django Example: FileField and ImageField</a></li>
<li><a href='http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/' rel='bookmark' title='Permanent Link: Debugging Django on Windows : Developement Setup'>Debugging Django on Windows : Developement Setup</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Debugging Django : Best Stuff on Web</title>
		<link>http://www.nitinh.com/2009/03/debugging-django-best-stuff-on-web/</link>
		<comments>http://www.nitinh.com/2009/03/debugging-django-best-stuff-on-web/#comments</comments>
		<pubDate>Sun, 22 Mar 2009 20:22:34 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[slide]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=186</guid>
		<description><![CDATA[TweetShareContinuing my previous post Debugging Django Application, in this post I am trying to aggregate best of web tutorial which will definitely help you while debugging any django application. Eric Holscher Video ScreenCasts Simon Willison Presentation Debugging Django View more presentations from simon. Here is my list please do share any link that is not [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/' rel='bookmark' title='Permanent Link: Debugging Django on Windows : Developement Setup'>Debugging Django on Windows : Developement Setup</a></li>
<li><a href='http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/' rel='bookmark' title='Permanent Link: Django ORM : What to do when the memory requirement goes high?'>Django ORM : What to do when the memory requirement goes high?</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-via="socializeWP" data-url="http://www.nitinh.com/2009/03/debugging-django-best-stuff-on-web/">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="socialize-in-button-left"><a name="fb_share" type="box_count" share_url="http://www.nitinh.com/2009/03/debugging-django-best-stuff-on-web/" href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div></div><p>Continuing my previous post <a href="http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/">Debugging Django Application</a>, in this post I am trying to aggregate best of web tutorial which will definitely help you while debugging any django application.<span id="more-186"></span></p>
<h4>Eric Holscher Video ScreenCasts</h4>
<p><object width="590" height="440"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=1615834&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=1615834&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="590" height="440"></embed></object><BR><BR></p>
<p><object width="590" height="440"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=1624166&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=1624166&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="590" height="440"></embed></object><BR><BR></p>
<p><object width="590" height="417"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=1636490&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=1636490&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="590" height="417"></embed></object><BR></p>
<h4><a href="http://simonwillison.net/2008/May/22/debugging/">Simon Willison Presentation</a></h4>
<div style="width:425px;text-align:left" id="__ss_420695"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/simon/debugging-django?type=powerpoint" title="Debugging Django">Debugging Django</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slideshare.net/swf/ssplayer2.swf?doc=debuggingdjango-1211412828499242-9&#038;rel=0&#038;stripped_title=debugging-django" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slideshare.net/swf/ssplayer2.swf?doc=debuggingdjango-1211412828499242-9&#038;rel=0&#038;stripped_title=debugging-django" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/simon">simon</a>.</div>
</div>
<p><BR>Here is my list please do share any link that is not listed here.</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/' rel='bookmark' title='Permanent Link: Debugging Django on Windows : Developement Setup'>Debugging Django on Windows : Developement Setup</a></li>
<li><a href='http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/' rel='bookmark' title='Permanent Link: Django ORM : What to do when the memory requirement goes high?'>Django ORM : What to do when the memory requirement goes high?</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2009/03/debugging-django-best-stuff-on-web/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Debugging Django on Windows : Developement Setup</title>
		<link>http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/</link>
		<comments>http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/#comments</comments>
		<pubDate>Sun, 22 Mar 2009 20:07:59 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[breakpoint]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[winpdb]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=179</guid>
		<description><![CDATA[TweetShare I remember the days when I used to struggle while debugging django web applications. But after working for more than a year now with django, I am more or less settled with tools I use for debugging Django Application. My Development System is on Windows XP, so this post is specifically for Debugging django [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/03/debugging-django-best-stuff-on-web/' rel='bookmark' title='Permanent Link: Debugging Django : Best Stuff on Web'>Debugging Django : Best Stuff on Web</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
<li><a href='http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/' rel='bookmark' title='Permanent Link: Implementing Search in Django Site using HayStack and Xapian / Whoosh'>Implementing Search in Django Site using HayStack and Xapian / Whoosh</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-via="socializeWP" data-url="http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="socialize-in-button-left"><a name="fb_share" type="box_count" share_url="http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/" href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div></div><p><img src="http://media.djangoproject.com/img/site/hdr_logo.gif" alt="Django" class="imgThumb" style="float:left;margin:5px 5px 0 0;" /> I remember the days when I used to struggle while debugging django web applications. But after working for more than a year now with django, I am more or less settled with tools I use for debugging Django Application. My Development System is on Windows XP, so this post is specifically for Debugging django on windows. </p>
<p><span id="more-179"></span></p>
<h3>Debugging Django on Windows XP</h3>
<p>The best tool I found on Windows environment for Django/ Python Debugging is : <a href="http://winpdb.org/download/"><b>WinPDB</b></a>, Just download and install.</p>
<h4>Steps to Debug a application</h4>
<h5>Launch WinPDB</h5>
<p>You'll find following screen after opening WinPDB.<BR></p>
<h5>Start App in WinPDB</h5>
<p>Launch an the application using File -> Launch. If For e.g. my application is in C:\projectHome\manage.py <BR>I will write launch application as:</p>
<pre>
C:\projectHome\manage.py runserver <b>--noreload</b>
</pre>
<p>Click on "GO" button on top toolbar.</p>
<h5>Put BreakPoints</h5>
<p>Open the affected file(file which needs debugging e.g. views.py), File->Open Source. Now put breakpoint in the source code by pressing F9. You are all set for debugging. Isn't that simple.<BR>Now go to browser and hit the URL which is causing problem. The debugger will stop execution on breakpoint. <BR><br />
<img class='imgThumb' src='http://imgur.com/98784.jpg' /></p>
<h5>Now What</h5>
<p> Now you are free to do anything for debugging, you can go for step execution if you think there is some error in login/code. You can also see inmemory objects. etc, etc.<BR><br />
<img src='http://imgur.com/986GC.jpg'  class="imgThumb" /><br />
<BR><BR><br />
For me WinPDB has solved everything, hope it will help you as well.</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/03/debugging-django-best-stuff-on-web/' rel='bookmark' title='Permanent Link: Debugging Django : Best Stuff on Web'>Debugging Django : Best Stuff on Web</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
<li><a href='http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/' rel='bookmark' title='Permanent Link: Implementing Search in Django Site using HayStack and Xapian / Whoosh'>Implementing Search in Django Site using HayStack and Xapian / Whoosh</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Django ORM : What to do when the memory requirement goes high?</title>
		<link>http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/</link>
		<comments>http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 15:06:19 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=70</guid>
		<description><![CDATA[TweetShareAs the title suggest this post is regarding "Memory Requirement of django ORM". Actually some days back I myself faced this problem and wrote "Why Django ORM Sucks : It takes a hell lot of memory in processing". From various comments and a thread which started on google groups, I came up with few points [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/' rel='bookmark' title='Permanent Link: Why Django ORM Sucks : It takes a hell lot of memory in processing.'>Why Django ORM Sucks : It takes a hell lot of memory in processing.</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
<li><a href='http://www.nitinh.com/2009/03/debugging-django-best-stuff-on-web/' rel='bookmark' title='Permanent Link: Debugging Django : Best Stuff on Web'>Debugging Django : Best Stuff on Web</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-via="socializeWP" data-url="http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="socialize-in-button-left"><a name="fb_share" type="box_count" share_url="http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/" href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div></div><p>As the title suggest this post is regarding "Memory Requirement of django ORM".<br />
Actually some days back I myself faced this problem and wrote "<a href="http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/">Why Django ORM Sucks : It takes a hell lot of memory in processing</a>". From various comments and a thread which started on <a href="http://groups.google.com/group/django-developers/browse_thread/thread/8dbf145ab8fc3d8f">google groups</a>, I came up with few points one should check while operating on large amount of data.</p>
<p><span id="more-70"></span></p>
<p>For showing points i'll use following example:</p>
<pre class='brush: python'>
for r in UserRating.objects.all():
	ratingDict = cache.get(r.movie.id)
	if ratingDict is None:
		cache.set(r.movie.id, [(r.user.id, r.rating)], 86400)
	else:
		ratingDict.append((r.user.id, r.rating))
		cache.set(r.movie.id, ratingDict, 86400)
</pre>
<p>So here are the mistakes in previous code. Make sure such mistakes are not with you code.</p>
<p><h4>Reduce DB Interaction</h4>
<p>Use of <b>r.movie.id, r.user.id</b> : As you can see that here ".id" is used just to get id of movie and user object. <b>In place of this r.user_id and r.movie_id should be used</b>.</p>
<p>
<h4>Always use paginator</h4>
<p>While handling large amount of data you should always use paginator. This will keep memory size limited.<br />
Here is the sample code for using paginator.</p>
<pre class='brush: python'>
p = Paginator(UserRating.objects.all(), 50000)
for i in p.page_range:
	page = p.page(i)
	for r in page.object_list:
		...
</pre>
<p>So here the memory requirement will be limited to 50000 rows.
</p>
<p><h4>Use Iterator()</h4>
<p>If your dataset is not really very huge, and you don't want to use paginator, use <b>Iterator()</b>.<br />
<b>QuerySet.__iter__()</b> : this is the default method of accessing query set.<br />
If we do UserRating.objects.all() it means UserRating.objects.all().__iter__()<br />
<b>QuerySet.Iterator() </b> :<br />
we should do UserRating.objects.all().iterator().<br />
Both of these iteration method do chunked reads from the DB. Here <b>__iter__ also caches results , so if you reiterate you don't do a second db query, whereas iterator() doesn't cache them.</b></p>
<h5>As iterator() doesn't caches them the memory consumption is kept low.</h5>
</p>
<p>Hope these points will help you as well.<br />
Keep Coding &#8230; and keep Djangoing</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/' rel='bookmark' title='Permanent Link: Why Django ORM Sucks : It takes a hell lot of memory in processing.'>Why Django ORM Sucks : It takes a hell lot of memory in processing.</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
<li><a href='http://www.nitinh.com/2009/03/debugging-django-best-stuff-on-web/' rel='bookmark' title='Permanent Link: Debugging Django : Best Stuff on Web'>Debugging Django : Best Stuff on Web</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django Example: FileField and ImageField</title>
		<link>http://www.nitinh.com/2009/02/django-example-filefield-and-imagefield/</link>
		<comments>http://www.nitinh.com/2009/02/django-example-filefield-and-imagefield/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 08:18:32 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[FileField]]></category>
		<category><![CDATA[ImageField]]></category>
		<category><![CDATA[Model]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=59</guid>
		<description><![CDATA[TweetShareThe syntax to add it in Model is: class MyModel(models.Model): video = FileField(upload_to=None[, max_length=100, **options]) Here upload_to is local filesystem path that will be appended to your MEDIA_ROOT setting to determine the value of the url(mymodel.video.url) attribute. Setting MEDIA_ROOT and upload_to If, MEDIA_ROOT = ‘/home/myname/files/ upload_to=’videos’ file: abc.flv results in: /home/myname/files/videos/abc.flv where as if, MEDIA_ROOT [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/' rel='bookmark' title='Permanent Link: Implementing Search in Django Site using HayStack and Xapian / Whoosh'>Implementing Search in Django Site using HayStack and Xapian / Whoosh</a></li>
<li><a href='http://www.nitinh.com/2009/03/scaling-your-frontend-far-futures-expire-headers/' rel='bookmark' title='Permanent Link: Scaling Your Frontend: Far-Futures Expire Headers'>Scaling Your Frontend: Far-Futures Expire Headers</a></li>
<li><a href='http://www.nitinh.com/2009/03/compress-css-jsjavascript-files-using-php/' rel='bookmark' title='Permanent Link: Compress CSS / JS(Javascript) files Using PHP'>Compress CSS / JS(Javascript) files Using PHP</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-via="socializeWP" data-url="http://www.nitinh.com/2009/02/django-example-filefield-and-imagefield/">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="socialize-in-button-left"><a name="fb_share" type="box_count" share_url="http://www.nitinh.com/2009/02/django-example-filefield-and-imagefield/" href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div></div><p>The syntax to add it in Model is:</p>
<pre class="brush: python">
class MyModel(models.Model):
	video = FileField(upload_to=None[, max_length=100, **options])
</pre>
<p><span id="more-59"></span></p>
<p>Here <strong>upload_to</strong> is local filesystem path that will be appended to your <strong>MEDIA_ROOT</strong> setting to determine the value of the <strong>url</strong>(mymodel.video.url) attribute.</p>
<h5>Setting MEDIA_ROOT and upload_to</h5>
<p>If,<br />
MEDIA_ROOT = ‘/home/myname/files/<br />
upload_to=’videos’<br />
file: abc.flv<br />
results in: /home/myname/files/videos/abc.flv</p>
<p>where as if,<br />
MEDIA_ROOT = '/home/myname/files/'<br />
upload_to='<b>/videos</b>'<br />
and file : abc.flv<br />
result will be saved in : '/videos/abc.flv' (i.e., root directory of filesystem)</p>
<p>You can also set <b>upload_to='videos/%Y/%m/%d'</b>. The '%Y/%m/%d' part of upload_to is strftime formatting; '%Y' is the four-digit year, '%m' is the two-digit month and '%d' is the two-digit day. If you upload a file on Jan. 15, 2007, it will be saved in the directory /home/myname/files/videos/2007/01/15.</p>
<p>Now here's how would you save the actual data. </p>
<pre class="brush: python">
from django.core.files.base import ContentFile
def save_file(request):
	mymodel = MyModel.objects.get(id=1)
	file_content = ContentFile(request.FILES['video'].read())
	mymodel.video.save(request.FILES['video'].name, file_content)
</pre>
<p>Hope the code is self explanatory. For further help do drop comment.</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/' rel='bookmark' title='Permanent Link: Implementing Search in Django Site using HayStack and Xapian / Whoosh'>Implementing Search in Django Site using HayStack and Xapian / Whoosh</a></li>
<li><a href='http://www.nitinh.com/2009/03/scaling-your-frontend-far-futures-expire-headers/' rel='bookmark' title='Permanent Link: Scaling Your Frontend: Far-Futures Expire Headers'>Scaling Your Frontend: Far-Futures Expire Headers</a></li>
<li><a href='http://www.nitinh.com/2009/03/compress-css-jsjavascript-files-using-php/' rel='bookmark' title='Permanent Link: Compress CSS / JS(Javascript) files Using PHP'>Compress CSS / JS(Javascript) files Using PHP</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2009/02/django-example-filefield-and-imagefield/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Why Django ORM Sucks : It takes a hell lot of memory in processing.</title>
		<link>http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/</link>
		<comments>http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 06:50:00 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[MemCached]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[problem]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/</guid>
		<description><![CDATA[TweetShareFor a project i wanted to keep one of my table in memcache. I tried to do it using Django ORM but it was taking a hell lot of memory. Let me tell u some details. Table Structure : This table has almost 1,300,000 rows id movie_id user_id rating 1 1 1 5.0 2 1 [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/' rel='bookmark' title='Permanent Link: Django ORM : What to do when the memory requirement goes high?'>Django ORM : What to do when the memory requirement goes high?</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
<li><a href='http://www.nitinh.com/2009/02/django-example-filefield-and-imagefield/' rel='bookmark' title='Permanent Link: Django Example: FileField and ImageField'>Django Example: FileField and ImageField</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-via="socializeWP" data-url="http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="socialize-in-button-left"><a name="fb_share" type="box_count" share_url="http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/" href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div></div><p>For a project i wanted to keep one of my table in memcache. I tried to do it using Django ORM but it was taking a hell lot of memory.</p>
<p><span id="more-25"></span></p>
<p>Let me tell u some details.</p>
<p><strong>Table Structure</strong> : This table has almost <strong>1,300,000 rows</strong></p>
<table style="border: 1px solid #aaaaaa; width: 100%; text-align: center;" border="0">
<thead>
<tr>
<td><strong>id</strong></td>
<td><strong>movie_id</strong></td>
<td><strong>user_id</strong></td>
<td><strong>rating</strong></td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>5.0</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>2</td>
<td>4.5</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>3</td>
<td>4.5</td>
</tr>
<tr>
<td>4</td>
<td>2</td>
<td>1</td>
<td>3.5</td>
</tr>
<tr>
<td>5</td>
<td>3</td>
<td>3</td>
<td>2.0</td>
</tr>
</tbody>
</table>
<p>All I wanted was to keep them in cache(memcached), with following format:<br />
memcache Key  : movie-id<br />
memcache Data : [ (user1:rating1), (user2:rating2) ]</p>
<h4>First Approach : Directly from DB</h4>
<pre class="brush: python">
for r in FlixsterUserRating.objects.all():
	ratingDict = cache.get(r.movie.id)
	if ratingDict is None:
		cache.set(r.movie.id, [(r.user.id, r.rating)], 86400)
	else:
		ratingDict.append((r.user.id, r.rating))
		cache.set(r.movie.id, ratingDict, 86400)
	del ratingDict
</pre>
<p>In this approach for processing every 1000 it was taking some <strong>5.1 secs</strong>. But here <strong>Memory Requirement</strong> was growing like hell.<br />
In windows this program memory requirement was growing at rate of <strong>34 MB / 10K of records</strong>. Don't ask about using linux. In linux it was taking some 900MB for just processing 50K records.<br />
So to process full data it would need :<br />
(34 MB/10,000)*(1,300,000) = 4420 MB = almost <strong>4Gigs</strong>&#8230;. No way I can't afford this approach.</p>
<h4>Second Approach : From database in Chunks</h4>
<pre class="brush: python">
for m in FlixsterMovie.objects.all():
	d = []
	for r in FlixsterUserRating.objects.filter(movie=m):
		d.append((r.user.id, r.rating))
		cache.set(m.id, d)
</pre>
<p>In this approach for processing every 1000 it was taking some <strong>2.0 secs</strong>.<br />
In windows this program memory requirement was growing at rate of <strong>8 MB / 10K of records</strong>.<br />
So to process full data it would need :<br />
(8 MB/10,000)*(1,300,000) = 1040 MB = almost <strong>1Gigs</strong>&#8230;. Still it is a lot.. My Development system has only 1.5 GB RAM.</p>
<h4>Third Approach : Fuck Database Use CSV Dump</h4>
<p>Finally I left using Database. Dumped my data in a CSV. And used following program.</p>
<pre class="brush: python">
for s in csv.reader(open(csv_filename)):
	movie_id = int(s[1])
	user_id = int(s[2])
	rating = float(s[3])
	ratingDict = cache.get(movie_id)
	if ratingDict is None:
		cache.set(movie_id, [(user_id, rating)], 86400)
	else:
		ratingDict.append((user_id, rating))
		cache.set(movie_id, ratingDict, 86400)
</pre>
<p>In this approach memory requirement was just : 17-18MB and it wasn't growing. Some relief. But, As expected this approach was very slow. For processing 1K records it was taking some <strong>55 Secs</strong>.<br />
So to perform full task it will take some : 55 * 1,300 ~ <strong>20Hrs.</strong></p>
<h6>Fuck, What should I do now</h6>
<p>I ran out of options.<br />
I am going ahead with 2nd approach. But suggestions from you guys are most welcome.</p>
<h4>Update</h4>
<p>Final Version:</p>
<pre class="brush: python">
p = Paginator(FlixsterUserRating.objects.all(), 50000)
for i in p.page_range:
	page = p.page(i)
	for r in page.object_list:
		ratingDict = cache.get(r.movie_id)
		if ratingDict is None:
			cache.set(r.movie_id, {r.user_id: r.rating}, 86400)
		else:
			ratingDict[r.user_id] =  r.rating
			cache.set(r.movie_id, ratingDict, 86400)
</pre>
<p>This approach kept the memory level below 100 MB, and whole process was completed in ~ 1hr.</p>
<p>Thanks you all&#8230;<br />
Moreover, I didn't put stat for Iterator version because there wasn't any significant improvement. Memory consumption was growing even in this case.</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/' rel='bookmark' title='Permanent Link: Django ORM : What to do when the memory requirement goes high?'>Django ORM : What to do when the memory requirement goes high?</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
<li><a href='http://www.nitinh.com/2009/02/django-example-filefield-and-imagefield/' rel='bookmark' title='Permanent Link: Django Example: FileField and ImageField'>Django Example: FileField and ImageField</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Installing Memcached for Django Application on Windows XP</title>
		<link>http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/</link>
		<comments>http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 04:18:00 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[MemCached]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[win32]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows XP]]></category>
		<category><![CDATA[windowsXP]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/</guid>
		<description><![CDATA[TweetShareFor a project I needed Memcached. But the problem was that I have my development system on WindowsXP. When I Google I got various links to install Memcached on windows, but there wasn't any link that tell all about Memcached with Django on Windows So Here we go: Download memcached 1.2.4 Win32. Unzip the binaries [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/' rel='bookmark' title='Permanent Link: Why Django ORM Sucks : It takes a hell lot of memory in processing.'>Why Django ORM Sucks : It takes a hell lot of memory in processing.</a></li>
<li><a href='http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/' rel='bookmark' title='Permanent Link: Debugging Django on Windows : Developement Setup'>Debugging Django on Windows : Developement Setup</a></li>
<li><a href='http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/' rel='bookmark' title='Permanent Link: Django ORM : What to do when the memory requirement goes high?'>Django ORM : What to do when the memory requirement goes high?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-via="socializeWP" data-url="http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="socialize-in-button-left"><a name="fb_share" type="box_count" share_url="http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/" href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div></div><p>For a project I needed Memcached. But the problem was that I have my development system on WindowsXP.</p>
<p><span id="more-24"></span></p>
<p>When I Google I got various links to install Memcached on windows, but there wasn't any link that tell all about <strong>Memcached with Django on Windows</strong></p>
<p>So Here we go:</p>
<li>Download <a href="http://www.splinedancer.com/memcached-win32/memcached-1.2.4-Win32-Preview-20080309_bin.zip"><a href="http://www.splinedancer.com/memcached-win32/memcached-1.2.4-Win32-Preview-20080309_bin.zip">memcached 1.2.4 Win32</a></a>.</li>
<li>Unzip the binaries in your desired directory (eg. c:\memcached)</li>
<li>Install the service using the command: 'c:\memcached\memcached.exe -d install' from either the command line</li>
<li>Start the server from the Microsoft Management Console or by running the following command: 'c:\memcached\memcached.exe -d start'</li>
<li>Use the server, by default listening to port 11211</li>
<li>Now we have to get something so that python can talk with our memcached server. For this we have various options, but the easiest is python-memcache</li>
<li>Grap latest file from <a href="ftp://ftp.tummy.com/pub/python-memcached/python-memcached-latest.tar.gz">python-memcached-latest.tar.gz</a></li>
<li>Extract the compressed files in a directory say C:\python-memcache\</li>
<li>Now Run "python setup.py install"</li>
<p>So till this point the installation part is done. Now the question comes how to use it in Django application.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">cache</span> <span style="color: #ff7700;font-weight:bold;">import</span> cache
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">test</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	cache_key = <span style="color: #483d8b;">'myID'</span>
	result = cache.<span style="color: black;">get</span><span style="color: black;">&#40;</span>qstr_key<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> result <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
		<span style="color: #483d8b;">&quot;&quot;&quot;Not Found in Cache&quot;&quot;&quot;</span>
		data = <span style="color: #483d8b;">'hello'</span>
		cache.<span style="color: #008000;">set</span><span style="color: black;">&#40;</span>cache_key, data, <span style="color: #ff4500;">86400</span><span style="color: black;">&#41;</span></pre></div></div>

<p>So Basically we have two functions here :<br />
<strong>cache.set<br />
cache.get</strong></p>
<p>Go and enjoy the benefit of memcached</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/02/why-django-orm-sucks-it-takes-a-hell-lot-of-memory-in-processing/' rel='bookmark' title='Permanent Link: Why Django ORM Sucks : It takes a hell lot of memory in processing.'>Why Django ORM Sucks : It takes a hell lot of memory in processing.</a></li>
<li><a href='http://www.nitinh.com/2009/03/debugging-django-on-windows-developement-setup/' rel='bookmark' title='Permanent Link: Debugging Django on Windows : Developement Setup'>Debugging Django on Windows : Developement Setup</a></li>
<li><a href='http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/' rel='bookmark' title='Permanent Link: Django ORM : What to do when the memory requirement goes high?'>Django ORM : What to do when the memory requirement goes high?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
