<?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>Peter Krantz &#187; django</title>
	<atom:link href="http://www.peterkrantz.com/category/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.peterkrantz.com</link>
	<description></description>
	<lastBuildDate>Sat, 04 Feb 2012 09:57:17 +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>Rails vs Grails vs Django models</title>
		<link>http://www.peterkrantz.com/2009/rails-grails-django-models/</link>
		<comments>http://www.peterkrantz.com/2009/rails-grails-django-models/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 16:58:47 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[domain modeling]]></category>
		<category><![CDATA[grails]]></category>

		<guid isPermaLink="false">http://www.peterkrantz.com/?p=281</guid>
		<description><![CDATA[Coming back to Rails after being away from some time in Django land I discovered a huge difference in how Rails and Django treats your models...]]></description>
			<content:encoded><![CDATA[<p>Coming back to Rails after being away from some time in Django land I discovered a huge difference in how Rails, Grails and Django treats your models. <span id="more-281"></span>In Django and Grails you can look at a model class and see all the properties it has:</p>
<pre class="brush: python; title: ; notranslate">
class Organization(models.Model):
    name = models.CharField(max_length=255)
    url = models.URLField(verify_exists=False)
    orgtype = models.ForeignKey(OrgType)
</pre>
<p>The same model class in Rails typically looks like this:</p>
<pre class="brush: ruby; title: ; notranslate">
class Organization &lt; ActiveRecord::Base
    belongs_to :OrgType
end
</pre>
<p>&#8230;and in Grails it is more specific like Django:</p>
<pre class="brush: groovy; title: ; notranslate">
class Organization {
  String name
  String url
  static belongsTo = OrgType
  OrgType orgtype
}
</pre>
<p>It took me a while to remember that in Rails, parts of the model design is actually stored in the database schema instead of in the Ruby code. Peculiar don&#8217;t you think, given that everything else in a Rails app is nicely declared in Ruby code? There are of course benefits to both approaches, but I have started adding comments in the Rails model classes to be able to remember what properties they have without peeking in the Db. Typically I have a number of half-baked projects on my laptop and from time to time I forget what they do and these comments help me remember.</p>
<p>Check out more examples here:</p>
<ul>
<li><a href="http://docs.djangoproject.com/en/dev/topics/db/models/#topics-db-models">Django models</a></li>
<li><a href="http://www.tutorialspoint.com/ruby-on-rails/rails-models.htm">Rails models</a></li>
<li><a href="https://www.ibm.com/developerworks/java/library/j-grails02128/">Grails models</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.peterkrantz.com/2009/rails-grails-django-models/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Integrating Yahoo Search in a Django site in 5 easy steps</title>
		<link>http://www.peterkrantz.com/2008/yahoo-search-in-django/</link>
		<comments>http://www.peterkrantz.com/2008/yahoo-search-in-django/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 12:38:17 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[boss]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yahoo boss]]></category>

		<guid isPermaLink="false">http://www.peterkrantz.com/?p=247</guid>
		<description><![CDATA[I have been experimenting with various search options for the eutveckling.se site for a while. Google Custom ...]]></description>
			<content:encoded><![CDATA[<p>I have been experimenting with various search options for the <a href="http://www.eutveckling.se/">eutveckling.se</a> site for a while. Google Custom Search is nice and very fast, but the number of ads appearing in the search result page makes it difficult for users to separate result items from ads. (Update: I am sticking with Google Custom Search until I figure out how to get Yahoo search to present proper excerpts).<span id="more-247"></span></p>
<p>I am a fast reader which comes with the tradeoff of missing important information sometimes. Skimming through the terms for using the API I was a bit disappointed at first. That was because I was only reading the first column in the table that lists the <strong>previous </strong>terms of use. Oh well. The second column that lists the current restrictions (or rather lack of restrictions) makes it clear that Yahoo search is very easy to get started with. It is almost so that you start wondering where Yahoo will make money from providing a service like that.</p>
<p>Anyway, here are the five simple steps to get Yahoo search integrated in your Django site:</p>
<h2>1. Get an API Key</h2>
<p>&#8230;or &#8220;application ID&#8221; as Yahoo calls it. Visit <a href="http://developer.yahoo.com/wsregapp/">this page to sign up for an API key</a>.</p>
<h2>2. Install the pYsearch library</h2>
<p>Download <a href="http://pysearch.sourceforge.net/">pYsearch</a> from Sourceforge to your computer (here is the <a href="http://garr.dl.sourceforge.net/sourceforge/pysearch/pYsearch-3.1.tar.gz">direct link to the package</a>), and:</p>
<p><kbd>tar xvf pYsearch-3.1.tar.gz</kbd></p>
<p><kbd>cd pYsearch</kbd></p>
<p><kbd>sudo python setup.py build</kbd></p>
<p><kbd>sudo python setup.py install</kbd></p>
<p>This should install pYsearch somewhere on your Python path.</p>
<h2>3. Set up URL-pattern and view method</h2>
<p>In your urls.py add a URL-pattern to pick up search requests. We&#8217;ll use queries like /search/?q=myquery:</p>
<pre>url(r'^search/$', 'myapp.views.search', name='site_search'),</pre>
<p>Set up the view method in your application&#8217;s views.py (make sure the search query is encoded to utf-8 to enable characters outside ISO-8859-1 in the query parameter):</p>
<pre>from yahoo.search.web import WebSearch

def search(request):
	query = request.GET.get("q", "").encode("utf-8")

	if len(query) &gt; 0:

		#Call yahoo!
		api_key = "[your api key]"
		srch = WebSearch(api_key)
		srch.site = "www.example.com" #restrict to your own site
		srch.query = query
		srch.results = 50
		result = srch.parse_results() #puts all result items into a dict

	return render_to_response('search/search.html', locals())</pre>
<h2>4. And now the search form and result template</h2>
<p>Add the search field and form somewhere on your site:</p>
<pre>&lt;form action="/search/" method="GET"&gt;
	&lt;div&gt;
	&lt;label for="q""&gt;Find:&lt;/label&gt;
	&lt;input type="text" name="q" accesskey="4" size="30" id="q" value="{{query}}"&gt;
	&lt;input type="submit" value="Search"&gt;
	&lt;/div&gt;
&lt;/form&gt;</pre>
<p>The base.html sets up the basic web page. This template lives in myapp/templates/search/search.html</p>
<pre>{% extends 'base.html' %}
{% block content %}
	{% if result.results %}
        &lt;ul class="searchresult"&gt;
	{% for item in result %}
	&lt;li&gt;
           &lt;h2&gt;&lt;a href="{{item.Url}}"&gt;{{item.Title}}&lt;/a&gt;&lt;/h2&gt;
           &lt;p&gt;{{item.Summary|cut:" ..."}}...&lt;/p&gt;
        &lt;/li&gt;
        {% endfor %}
        &lt;/ul&gt;
{% else %}
Suggestion:
&lt;ul&gt;
	&lt;li&gt;Check your spelling.&lt;/li&gt;
	&lt;li&gt;Try similar words.&lt;/li&gt;
&lt;/ul&gt;
{% endif %}
{% endblock %}</pre>
<h2>5. There is no step 5!</h2>
<p>That&#8217;s it! Your site should now have a nice search engine. Some issues you may encounter:</p>
<ol>
<li>Yahoo does not seem to have indexed all of <a href="http://www.eutveckling.se">eutveckling.se</a> yet. This means that the result set will be limited. No PDF documents seem to have been included (<a href="http://www.eutveckling.se/sok/?q=V%C3%A4gledningen+24-timmarswebben">searching for &#8220;Vägledningen 24-timmarswebben&#8221;</a> does not return <a href="http://www.eutveckling.se/sok/?q=V%C3%A4gledningen+24-timmarswebben">the PDF document</a> even though it has many links on the site). It would be nice to be able to see how much of the site that the search engine knows about.</li>
<li>The summary text for each page seems to be the same (including hidden skiplink text). This may be my fault as I haven&#8217;t provided meta description elements yet. I have added som CSS classnames (robots-nocontent) to navigation elements to help the search engine decide on what should be included and what should be skipped. I had expected that the summary would contain a phrase close to the query term instead of text from the top of the page.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.peterkrantz.com/2008/yahoo-search-in-django/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Improving Django performance with better caching</title>
		<link>http://www.peterkrantz.com/2008/improving-django-cache-performance/</link>
		<comments>http://www.peterkrantz.com/2008/improving-django-cache-performance/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 18:58:54 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.peterkrantz.com/?p=231</guid>
		<description><![CDATA[The Django cache middleware is great, but has one drawback. If you are caching views (which can ...]]></description>
			<content:encoded><![CDATA[<p>The Django cache middleware is great, but has one drawback. If you are caching views (which can give a nice performance boost) Django will only use the path segment of the URL to create a cache key. If you are an avid reader of <a href="http://labs.apache.org/webarch/uri/rfc/rfc3986.html#components">RFC 3986</a> you may remember that a URI consists of multiple components; path and query being of special interest here. The problem is documented in <a href="http://code.djangoproject.com/ticket/4992">ticket 4992</a> (<strong>Update</strong>: it is not in Django).<span id="more-231"></span></p>
<p>Given the following URL:</p>
<pre>http://example.com/items/?order_by=name</pre>
<p>&#8230;Django will ignore the query part when determining a cache key so the key for the above request will be:</p>
<pre>/items/</pre>
<p>Any type of query parameter will make Django ignore the cached page. Django will not create a new cache item for the order_by request. This will be detrimental for the performance of your site. I had expected Django to create three different cache items for these URLs instead of one cache item and two ignored requests:</p>
<ul>
<li>
<pre>http://example.com/items/</pre>
</li>
<li>
<pre>http://example.com/items/?order_by=name</pre>
</li>
<li>
<pre>http://example.com/items/?order_by=date</pre>
</li>
</ul>
<h2>The solution</h2>
<p>Fortunately Django&#8217;s source is very readable and easy to adapt to your own needs. A few minor changes to middleware/cache.py and utils/cache.py and you are ready to go. For details see the <a href="http://code.djangoproject.com/attachment/ticket/4992/cache_by_request_full_path.diff">patch I attached to ticket 4992</a>.</p>
<p>I recently deployed a Django site to get an <a href="http://www.comparevps.com/">overview of VPS hosting plans</a> and the ability to cache items based on the filtering parameters gives much better performance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterkrantz.com/2008/improving-django-cache-performance/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.peterkrantz.com @ 2012-02-04 11:23:53 -->
