<?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>Indirecthit &#187; Django</title>
	<atom:link href="http://www.indirecthit.com/category/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.indirecthit.com</link>
	<description>A Discussion on PHP, AJAX and Other Web Tools with a bit of startup talk</description>
	<lastBuildDate>Tue, 02 Mar 2010 14:34:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Django Caching Middleware &amp; Login Page</title>
		<link>http://www.indirecthit.com/2008/06/02/django-caching-middleware-login-page/</link>
		<comments>http://www.indirecthit.com/2008/06/02/django-caching-middleware-login-page/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 13:15:48 +0000</pubDate>
		<dc:creator>clong</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[StartupIndex]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[cache]]></category>

		<guid isPermaLink="false">http://www.indirecthit.com/?p=37</guid>
		<description><![CDATA[Recently on StartupIndex.ca, I&#8217;ve noticed a problem where the first time you login it fails, the second time it works. I sat down last night, and spent some time attempting to solve this problem. My search on the greater web didn&#8217;t end up with very many results, I did find one mailing list thread on [...]]]></description>
			<content:encoded><![CDATA[<p>Recently on StartupIndex.ca, I&#8217;ve noticed a problem where the first time you login it fails, the second time it works. I sat down last night, and spent some time attempting to solve this problem. My search on the greater web didn&#8217;t end up with very many results, I did find one mailing list thread on &#8220;<a href="http://groups.google.ca/group/django-users/browse_thread/thread/3d0694201341c205/abf83324933b243f?hl=en&amp;lnk=st&amp;q=django+login+auth+fails+multiple#abf83324933b243f">django.contrib.auth.views.login failing cookie test</a>&#8220;. This wasn&#8217;t exactly what I was looking for, but it set me down the right path.</p>
<p>Django comes with a <a href="http://www.djangoproject.com/documentation/cache/#the-per-site-cache">caching middleware</a> that caches any request made to the site that doesn&#8217;t have a POST or GET. You can set the cache to only cache pages for anonymous (using CACHE_MIDDLEWARE_ANONYMOUS_ONLY). The second piece of the puzzle is that the login view for Django sets a test cookie when a visitor first comes to the page, if this is not there, the login will fail.</p>
<p>What happens is:</p>
<ol>
<li>Make a request to the login page without any GET or POST parameters.</li>
<li>Web server returns the cached page. (Note: this doesn&#8217;t set the cookie because no Python code has been run, the page returned is static)</li>
<li>The user enters in the login information and submits the information. This is sent to the server as a POST.</li>
<li>The server, because of the POST, now runs the login view. This fails because there was no cookie previously set, but it does set the cookie that should have been previously set.</li>
<li>The error message shown to the user is as if it was a failed attempt. If the user reenters the information, the login will now work as the cookie is now set.</li>
</ol>
<p>Hopefully that makes some sense.</p>
<p>What is the solution? Unfortunately, the <a href="http://www.djangoproject.com/documentation/cache/#the-per-site-cache">caching middleware</a> doesn&#8217;t allow you to fine-grain what it caches, it&#8217;s an all or nothing. There are two solutions:</p>
<ol>
<li>Always pass a GET param to the login page. By passing a GET param, the <a href="http://www.djangoproject.com/documentation/cache/#the-per-site-cache">caching middleware</a> will not return the cached page.</li>
<li>Turn off the  <a href="http://www.djangoproject.com/documentation/cache/#the-per-site-cache">caching middleware</a> and instead use the more fine-grained <a href="http://www.djangoproject.com/documentation/cache/#the-per-view-cache">per-view cache</a>.</li>
</ol>
<p>I&#8217;m trying the GET parameter. We&#8217;ll see how it works.</p>]]></content:encoded>
			<wfw:commentRss>http://www.indirecthit.com/2008/06/02/django-caching-middleware-login-page/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Django: Difference Between Two Model Instances</title>
		<link>http://www.indirecthit.com/2008/04/29/django-difference-between-two-model-instances/</link>
		<comments>http://www.indirecthit.com/2008/04/29/django-difference-between-two-model-instances/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 18:48:19 +0000</pubDate>
		<dc:creator>clong</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.indirecthit.com/?p=30</guid>
		<description><![CDATA[Just a quick snippet, I call this code on a site I&#8217;m working on to handle some basic moderation. It finds the difference between the two instances, and returns a dictionary of the changes. The key in the dictionary is the field name, each value in the dictionary is a tuple containing the new and [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick snippet, I call this code on a site I&#8217;m working on to handle some basic moderation. It finds the difference between the two instances, and returns a dictionary of the changes. The key in the dictionary is the field name, each value in the dictionary is a tuple containing the new and old value of the field.</p>
<p>You can pass it the excludes field to exclude certain fields from being compared. It does not compare AutoField or RelatedField&#8217;s.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span>.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> fields
<span style="color: #ff7700;font-weight:bold;">def</span> get_changes_between_models<span style="color: black;">&#40;</span>model1, model2, excludes = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
    changes = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> field <span style="color: #ff7700;font-weight:bold;">in</span> model1._meta.<span style="color: black;">fields</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: black;">&#40;</span><span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>field, <span style="color: black;">&#40;</span>fields.<span style="color: black;">AutoField</span>, fields.<span style="color: black;">related</span>.<span style="color: black;">RelatedField</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> 
                <span style="color: #ff7700;font-weight:bold;">or</span> field.<span style="color: black;">name</span> <span style="color: #ff7700;font-weight:bold;">in</span> excludes<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> field.<span style="color: black;">value_from_object</span><span style="color: black;">&#40;</span>model1<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= field.<span style="color: black;">value_from_object</span><span style="color: black;">&#40;</span>model2<span style="color: black;">&#41;</span>:
                changes<span style="color: black;">&#91;</span>field.<span style="color: black;">verbose_name</span><span style="color: black;">&#93;</span> = <span style="color: black;">&#40;</span>field.<span style="color: black;">value_from_object</span><span style="color: black;">&#40;</span>model1<span style="color: black;">&#41;</span>,
                                                   field.<span style="color: black;">value_from_object</span><span style="color: black;">&#40;</span>model2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> changes</pre></td></tr></table></div>

<p><em>Based off of code from <a href="http://code.google.com/p/django-modelhistory/">Django Modelhistory</a>.</em></p>]]></content:encoded>
			<wfw:commentRss>http://www.indirecthit.com/2008/04/29/django-difference-between-two-model-instances/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django: Caching Querysets</title>
		<link>http://www.indirecthit.com/2008/04/28/django-caching-querysets/</link>
		<comments>http://www.indirecthit.com/2008/04/28/django-caching-querysets/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 16:40:21 +0000</pubDate>
		<dc:creator>clong</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.indirecthit.com/?p=28</guid>
		<description><![CDATA[I have just recently started working with Django again, its been a while since I last used it. One thing I was trying to do was cache querysets, which doesn&#8217;t work. Before the recent merge of the queryset refactor branch into trunk, caching querysets appeared to work, in reality it didn&#8217;t. Now with the merge [...]]]></description>
			<content:encoded><![CDATA[<p>I have just recently started working with Django again, its been a while since I last used it. One thing I was trying to do was cache querysets, which doesn&#8217;t work. Before the recent merge of the queryset refactor branch into trunk, caching querysets appeared to work, in reality it didn&#8217;t. Now with the merge of queryset refactor branch, it fails with a Python error. <a href="http://www.pointy-stick.com/blog/">Malcom</a> (the author of the queryset refactor branch) wrote up the reason for this on the django mailing list in <a href="http://groups.google.com/group/django-users/browse_thread/thread/32143d024b17dd00/cc0428d9c520fa1d?lnk=gst&amp;q=cache+queryset#cc0428d9c520fa1d">this thread</a>.</p>
<p>The solution? Force the queryset to a list. For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">queryset = Widget.<span style="color: black;">objects</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
wlist = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>queryset<span style="color: black;">&#41;</span>
cache.<span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;allwidgets&quot;</span>, wlist, <span style="color: #ff4500;">3600</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>This does mean that you can&#8217;t use the cached list as a queryset, so you&#8217;ll have to cache it after all the filtering, ordering, etc. has been done.</p>]]></content:encoded>
			<wfw:commentRss>http://www.indirecthit.com/2008/04/28/django-caching-querysets/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
