<?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>Chirashi Security &#187; Django</title>
	<atom:link href="http://chirashi.zensay.com/category/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://chirashi.zensay.com</link>
	<description>a blog with scattered thoughts on security.</description>
	<lastBuildDate>Sun, 25 Jul 2010 05:25:30 +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>Sending messages to the Apache Error.log file when using Django</title>
		<link>http://chirashi.zensay.com/2009/05/sending-messages-to-the-apache-errorlog-file-when-using-django/</link>
		<comments>http://chirashi.zensay.com/2009/05/sending-messages-to-the-apache-errorlog-file-when-using-django/#comments</comments>
		<pubDate>Sun, 31 May 2009 08:27:57 +0000</pubDate>
		<dc:creator>chopstick</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[HOWTO]]></category>

		<guid isPermaLink="false">http://chirashi.zensay.com/?p=23</guid>
		<description><![CDATA[Solves the problem of "sys.stdout access restricted by mod_wsgi"]]></description>
			<content:encoded><![CDATA[<p>I spent a considerable amount of time reading documentation that told me how to send errors to the Apache log filewhen you use mod_wsgi. I read documentation on how to integrate mod_wsgi with Apache and Django.  All of this was fine, but when I was trying to get it to work with my Django 1.0.2 installation and Apache2 running mod_wsgi, I was constantly greeted by lots of errors.  One in particular was &#8220;sys.stdout access restricted by mod_wsgi&#8221;.  The documents online didn&#8217;t help at all.</p>
<p>Finally, after looking into the django.core.handlers.wsgi module, I figured out how to send errors to the wsgi.errors setting and subsequently send errors to your Apache2 Error.log file.</p>
<p>I started off with my views.py as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</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;">http</span> <span style="color: #ff7700;font-weight:bold;">import</span> HttpResponse
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">shortcuts</span> <span style="color: #ff7700;font-weight:bold;">import</span> render_to_response, get_object_or_404
<span style="color: #ff7700;font-weight:bold;">from</span> myapp.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> F
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
    c = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">'myapp/index.html'</span>,c<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> detail<span style="color: black;">&#40;</span>request, name<span style="color: black;">&#41;</span>:
    n = F.<span style="color: black;">objects</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>name=name<span style="color: black;">&#41;</span>
    c = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'name'</span>: n.<span style="color: black;">fof</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">'myapp/detail.html'</span>,c<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>If I needed to write to the Error.log file, I would have to change the code thusly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</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;">http</span> <span style="color: #ff7700;font-weight:bold;">import</span> HttpResponse
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">shortcuts</span> <span style="color: #ff7700;font-weight:bold;">import</span> render_to_response, get_object_or_404
<span style="color: #ff7700;font-weight:bold;">from</span> myapp.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> F
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
    c = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #66cc66;">&gt;&gt;</span> request.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'wsgi.errors'</span><span style="color: black;">&#93;</span>,<span style="color: #483d8b;">&quot;Teh Errorist!&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">'myapp/index.html'</span>,c<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> detail<span style="color: black;">&#40;</span>request, name<span style="color: black;">&#41;</span>:
    n = F.<span style="color: black;">objects</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>name=name<span style="color: black;">&#41;</span>
    c = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'name'</span>: n.<span style="color: black;">fof</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">'myapp/detail.html'</span>,c<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The change is in line 7</p>
<p>I felt really stupid, because the solution was so straightforward.  I felt even more stupid that I couldn&#8217;t find any documentation out there.  Either way, I&#8217;m pleased that my app works, and in case someone else out there is searching for how to achieve this, then the above is how to do it.</p>



Share this on:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fchirashi.zensay.com%2F2009%2F05%2Fsending-messages-to-the-apache-errorlog-file-when-using-django%2F&amp;title=Sending%20messages%20to%20the%20Apache%20Error.log%20file%20when%20using%20Django&amp;bodytext=Solves%20the%20problem%20of%20%22sys.stdout%20access%20restricted%20by%20mod_wsgi%22" title="Digg"><img src="http://chirashi.zensay.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fchirashi.zensay.com%2F2009%2F05%2Fsending-messages-to-the-apache-errorlog-file-when-using-django%2F&amp;title=Sending%20messages%20to%20the%20Apache%20Error.log%20file%20when%20using%20Django&amp;notes=Solves%20the%20problem%20of%20%22sys.stdout%20access%20restricted%20by%20mod_wsgi%22" title="del.icio.us"><img src="http://chirashi.zensay.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fchirashi.zensay.com%2F2009%2F05%2Fsending-messages-to-the-apache-errorlog-file-when-using-django%2F&amp;t=Sending%20messages%20to%20the%20Apache%20Error.log%20file%20when%20using%20Django" title="Facebook"><img src="http://chirashi.zensay.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fchirashi.zensay.com%2F2009%2F05%2Fsending-messages-to-the-apache-errorlog-file-when-using-django%2F&amp;title=Sending%20messages%20to%20the%20Apache%20Error.log%20file%20when%20using%20Django&amp;annotation=Solves%20the%20problem%20of%20%22sys.stdout%20access%20restricted%20by%20mod_wsgi%22" title="Google Bookmarks"><img src="http://chirashi.zensay.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fchirashi.zensay.com%2F2009%2F05%2Fsending-messages-to-the-apache-errorlog-file-when-using-django%2F&amp;title=Sending%20messages%20to%20the%20Apache%20Error.log%20file%20when%20using%20Django" title="Reddit"><img src="http://chirashi.zensay.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fchirashi.zensay.com%2F2009%2F05%2Fsending-messages-to-the-apache-errorlog-file-when-using-django%2F&amp;title=Sending%20messages%20to%20the%20Apache%20Error.log%20file%20when%20using%20Django" title="StumbleUpon"><img src="http://chirashi.zensay.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fchirashi.zensay.com%2F2009%2F05%2Fsending-messages-to-the-apache-errorlog-file-when-using-django%2F&amp;t=Sending%20messages%20to%20the%20Apache%20Error.log%20file%20when%20using%20Django&amp;s=Solves%20the%20problem%20of%20%22sys.stdout%20access%20restricted%20by%20mod_wsgi%22" title="Tumblr"><img src="http://chirashi.zensay.com/wp-content/plugins/sociable/images/tumblr.png" title="Tumblr" alt="Tumblr" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Sending%20messages%20to%20the%20Apache%20Error.log%20file%20when%20using%20Django%20-%20http%3A%2F%2Fchirashi.zensay.com%2F2009%2F05%2Fsending-messages-to-the-apache-errorlog-file-when-using-django%2F" title="Twitter"><img src="http://chirashi.zensay.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Sending%20messages%20to%20the%20Apache%20Error.log%20file%20when%20using%20Django&amp;url=http%3A%2F%2Fchirashi.zensay.com%2F2009%2F05%2Fsending-messages-to-the-apache-errorlog-file-when-using-django%2F" title="Slashdot"><img src="http://chirashi.zensay.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://chirashi.zensay.com/2009/05/sending-messages-to-the-apache-errorlog-file-when-using-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
