<?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>Code That Matters &#187; pylons</title>
	<atom:link href="http://www.codethatmatters.com/tag/pylons/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codethatmatters.com</link>
	<description>Pylons, rails, jQuery and other stuff that matters</description>
	<lastBuildDate>Sat, 16 Jul 2011 10:57:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Check for ajax requests in Pylons</title>
		<link>http://www.codethatmatters.com/2010/01/check-for-ajax-requests-in-pylons/</link>
		<comments>http://www.codethatmatters.com/2010/01/check-for-ajax-requests-in-pylons/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 15:00:11 +0000</pubDate>
		<dc:creator>jorgen</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[is_xhr]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[xmlHttpRequest]]></category>

		<guid isPermaLink="false">http://www.codethatmatters.com/?p=132</guid>
		<description><![CDATA[In todays world of web development Ajax is taken for granted and there is a ton of good JavaScript frameworks out there that makes Ajax development quite easy and enjoyable. Together with a nice MVC framework it is even better. Each framework has its own way of detecting Ajax calls or more correctly xmlHttpRequests, which [...]]]></description>
			<content:encoded><![CDATA[<p>In todays world of web development Ajax is taken for granted and there is a ton of good JavaScript frameworks out there that makes Ajax development quite easy and enjoyable. Together with a nice MVC framework it is even better. Each framework has its own way of detecting Ajax calls or more correctly xmlHttpRequests, which is a huge topic on its own.</p>
<p>For checking if a request is an Ajax request in Pylons you just use is_xhr in your controller, this is based on the fact that you are running your Pylons app as wsgi.</p>
<p>Make sure your importing the request class</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span class="kw1">from</span> pylons <span class="kw1">import</span> request</pre></div></div>

<p>Then in your controller function you can just do</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span class="co1"># Checks if request is a xmlHttpRequest (Ajax) and returns a def</span>
<span class="kw1">if</span> request.<span class="me1">is_xhr</span>:
&nbsp;
<span class="co1"># Code goes here</span>
&nbsp;
<span class="kw1">return</span> render_mako_def<span class="br0">&#40;</span><span class="st0">'template_path'</span>,<span class="st0">&quot;def_name&quot;</span><span class="br0">&#41;</span></pre></div></div>

<p>You can also return and render a full template if you need or think that works better with your function.</p>
<p>More information can be found at <a href="http://pylonshq.com/docs/en/0.9.7/advanced_pylons/paster/">PylonsHQ Paster &#8211; Documentation &#8211; WSGI, CLI scripts</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codethatmatters.com/2010/01/check-for-ajax-requests-in-pylons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Redirect to previous url after delete</title>
		<link>http://www.codethatmatters.com/2009/10/redirect-to-previous-url-after-delete/</link>
		<comments>http://www.codethatmatters.com/2009/10/redirect-to-previous-url-after-delete/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 14:03:31 +0000</pubDate>
		<dc:creator>henke</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[request]]></category>

		<guid isPermaLink="false">http://www.codethatmatters.com/?p=92</guid>
		<description><![CDATA[Ever wondered how you can easily redirect a user to the previous url? I.e after a users clicks a delete link or some other action you can use the more common url.current() because that points to the delete action. Instead use request.header REFERER like this:
UPDATE: New way of doing this in pylons 1.0 as redirect_to [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wondered how you can easily redirect a user to the previous url? I.e after a users clicks a delete link or some other action you can use the more common url.current() because that points to the delete action. Instead use request.header REFERER like this:<br />
UPDATE: New way of doing this in pylons 1.0 as redirect_to is replaced by redirect. <em>Thanks to DB for pointing that out!</em></p>
<p>(This is a Pylons example)</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">redirect<span class="br0">&#40;</span>url<span class="br0">&#40;</span>request.<span class="me1">referer</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre></div></div>

<p>Done!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codethatmatters.com/2009/10/redirect-to-previous-url-after-delete/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unicode in Mako using Pylons</title>
		<link>http://www.codethatmatters.com/2009/10/unicode-in-mako-using-pylons/</link>
		<comments>http://www.codethatmatters.com/2009/10/unicode-in-mako-using-pylons/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 07:18:02 +0000</pubDate>
		<dc:creator>henke</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[mako]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://www.codethatmatters.com/?p=72</guid>
		<description><![CDATA[Small post regarding unicode support in mako. I learned yesterday that mako not only has input and output encoding options but also an default_filter encoding. In the environment.py file, in your Pylons config folder, you need all these three to be sure that everything is enoded as unicode. Remember though that the default_filters argument can [...]]]></description>
			<content:encoded><![CDATA[<p>Small post regarding unicode support in mako. I learned yesterday that mako not only has input and output encoding options but also an default_filter encoding. In the environment.py file, in your Pylons config folder, you need all these three to be sure that everything is enoded as unicode. Remember though that the default_filters argument can have big impact on performance.</p>
<p><em>(If someone knows the configuration files for other frameworks, please write a comment about it and I&#8217;ll add it to the post)</em></p>
<p>Please read the Mako unicode chapter <a href="http://www.makotemplates.org/docs/unicode.html">here</a> if you want the gory details.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span class="co1">#environment.py</span>
 config<span class="br0">&#91;</span><span class="st0">'pylons.app_globals'</span><span class="br0">&#93;</span>.<span class="me1">mako_lookup</span> = TemplateLookup<span class="br0">&#40;</span>
        input_encoding=<span class="st0">'utf-8'</span>, 
        output_encoding=<span class="st0">'utf-8'</span>, 
        default_filters=<span class="br0">&#91;</span><span class="st0">'decode.utf8'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codethatmatters.com/2009/10/unicode-in-mako-using-pylons/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Serving images directly from database</title>
		<link>http://www.codethatmatters.com/2009/10/serving-images-directly-from-database/</link>
		<comments>http://www.codethatmatters.com/2009/10/serving-images-directly-from-database/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 08:54:13 +0000</pubDate>
		<dc:creator>henke</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.codethatmatters.com/?p=26</guid>
		<description><![CDATA[Even though storing images in the database have its advantages/disadvantages, here is a small guide for those who want to store them in the database and serve them directly to the browser without having to generate a file first. This examples is based on the amazing web framework pylons. 
In this example I want to [...]]]></description>
			<content:encoded><![CDATA[<p>Even though storing images in the database have its advantages/disadvantages, here is a small guide for those who want to store them in the database and serve them directly to the browser without having to generate a file first. This examples is based on the amazing web framework <a href="http://www.pylonshq.com">pylons</a>. </p>
<p>In this example I want to serve small product images from my product controller.</p>
<p>In the product.py controller I have this method</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span class="kw1">def</span> show_image<span class="br0">&#40;</span><span class="kw2">self</span>, <span class="kw2">id</span>, name<span class="br0">&#41;</span>:
    response.<span class="me1">headers</span><span class="br0">&#91;</span><span class="st0">'Content-type'</span><span class="br0">&#93;</span> = <span class="st0">'image/png'</span>
    response.<span class="me1">headers</span><span class="br0">&#91;</span><span class="st0">'Cache-Control'</span><span class="br0">&#93;</span> = <span class="st0">'max-age=14400'</span>
    response.<span class="me1">headers</span><span class="br0">&#91;</span><span class="st0">'Pragma'</span><span class="br0">&#93;</span> = <span class="st0">''</span>
    product = Product.<span class="me1">get_by</span><span class="br0">&#40;</span><span class="kw2">id</span>=<span class="kw2">id</span><span class="br0">&#41;</span>
    <span class="kw1">return</span> product.<span class="me1">image</span></pre></div></div>

<p>Then in my mako template I have</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;img</span> <span class="re0">src</span>=<span class="st0">&quot;url(controller='product', action='show_image', id='${c.product.id}', name='c.product.image_name')&quot;</span><span class="re2">/&gt;</span></span></pre></div></div>

<p>which will generate</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;img</span> <span class="re0">src</span>=<span class="st0">&quot;/product/show_image/1/super-glue.png&quot;</span><span class="re2">/&gt;</span></span></pre></div></div>

<p>So whats happening is that when the page is loading the image tag, the show_image() function is called which fetches the image data from the database and returns the image data buffer to the template. The key for this to work is to set the appropriate headers so that the page know that the data returned is image/png through the Content-type. The name argument is just used to set a name for the image file on the connecting client so that the cached file will have a correct name.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codethatmatters.com/2009/10/serving-images-directly-from-database/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

