<?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>mdgArt &#187; Django</title>
	<atom:link href="http://www.mdgart.com/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mdgart.com</link>
	<description>Art &#38; Programming</description>
	<lastBuildDate>Thu, 12 Jan 2012 11:56:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Django Auth behind proxy server</title>
		<link>http://www.mdgart.com/2011/01/15/django-auth-behind-proxy-server/</link>
		<comments>http://www.mdgart.com/2011/01/15/django-auth-behind-proxy-server/#comments</comments>
		<pubDate>Sat, 15 Jan 2011 17:35:44 +0000</pubDate>
		<dc:creator>Mauro</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Auth]]></category>
		<category><![CDATA[django admin]]></category>
		<category><![CDATA[Proxy]]></category>
		<category><![CDATA[SESSION]]></category>

		<guid isPermaLink="false">http://www.mdgart.com/?p=136</guid>
		<description><![CDATA[Yesterday I had a weird problem with a Django application that should primarily work on computer behind a proxy server: for some reason that I didn&#8217;t understand yet, the proxy lost the session&#8217;s cookie, but only when a form send data (via POST) to a view that is visible only to logged users (=O).
The other [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I had a weird problem with a Django application that should primarily work on computer behind a proxy server: for some reason that I didn&#8217;t understand yet, the proxy lost the session&#8217;s cookie, but only when a form send data (via POST) to a view that is visible only to logged users (=O).<br />
The other views works well, but that particular views lost the cookie! What to do in this case?<br />
A simple workaround helped me: I sent the session id via GET to the view. I know, django never use this for security reason, but I didn&#8217;t find a &#8220;official&#8221; solution, so this is what I did:</p>
<p>I create this middleware that use the query string session id if it doesn&#8217;t find the session&#8217;s cookies in the request:</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;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> FakeSessionCookieMiddleware<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> process_request<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> request.<span style="color: black;">COOKIES</span>.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span>settings.<span style="color: black;">SESSION_COOKIE_NAME</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> request.<span style="color: black;">GET</span>.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span>settings.<span style="color: black;">SESSION_COOKIE_NAME</span><span style="color: black;">&#41;</span>:
            request.<span style="color: black;">COOKIES</span><span style="color: black;">&#91;</span>settings.<span style="color: black;">SESSION_COOKIE_NAME</span><span style="color: black;">&#93;</span> = request.<span style="color: black;">GET</span><span style="color: black;">&#91;</span>settings.<span style="color: black;">SESSION_COOKIE_NAME</span><span style="color: black;">&#93;</span></pre></div></div>

<p>You have to add this middleware to your settings.py <strong>before</strong> django.contrib.sessions.middleware.SessionMiddleware:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">MIDDLEWARE_CLASSES = <span style="color: black;">&#40;</span>
    ...
    <span style="color: #483d8b;">'myapp.middleware.FakeSessionCookieMiddleware'</span>,
    <span style="color: #483d8b;">'django.contrib.sessions.middleware.SessionMiddleware'</span>,
    <span style="color: #483d8b;">'django.contrib.auth.middleware.AuthenticationMiddleware'</span>,
    ...
<span style="color: black;">&#41;</span></pre></div></div>

<p>In this case the middleware class is inside the <strong>middleware.py</strong> file in <strong>myapp</strong> application.<br />
Then you can add SESSION_COOKIE_NAME in your context like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">context = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'session_cookie_name'</span>: settings.<span style="color: black;">SESSION_COOKIE_NAME</span>,
    <span style="color: #483d8b;">'session_cookie_value'</span>: request.<span style="color: black;">COOKIES</span><span style="color: black;">&#91;</span>settings.<span style="color: black;">SESSION_COOKIE_NAME</span><span style="color: black;">&#93;</span>, 
<span style="color: black;">&#125;</span> 
    template = <span style="color: #483d8b;">'yourtemplate.html'</span> 	
    <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span>template, context, context_instance=RequestContext<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>and pass &#8220;session_cookie_name&#8221; and &#8220;session_cookie_value&#8221; in your URL:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;/your/url/?{{session_cookie_name}}={{session_cookie_value}}&quot;&gt;</pre></div></div>

<p>Is ugly and potentially dangerous, but it&#8217;s an extreme solution in case you REALLY have this problem that can&#8217;t be solved in other ways. Hope this can help someone with the same issue.</p>
<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="mauro@ondadiluce.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy me a coffe for Django Auth behind proxy server" /><input type="hidden" name="currency_code" value="EUR" /><input type="hidden" name="amount" value="1" /><input type="image" src="http://www.mdgart.com/wp-content/plugins/buy-me-beer/icon_cafe.gif" align="left" alt="Buy me a coffe" title="Buy me a coffe" hspace="3" style="border:0;" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=mauro@ondadiluce.com&amp;currency_code=EUR&amp;amount=1&amp;return=&amp;item_name=Buy+me+a+coffe+for+Django+Auth+behind+proxy+server" target="paypal">Donate 1 euro, buy me a coffee, I need it to write more posts! Thanks ;)</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mdgart.com/2011/01/15/django-auth-behind-proxy-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django Tip: Cross Domain Cookies</title>
		<link>http://www.mdgart.com/2011/01/15/django-tip-cross-domain-cookies/</link>
		<comments>http://www.mdgart.com/2011/01/15/django-tip-cross-domain-cookies/#comments</comments>
		<pubDate>Sat, 15 Jan 2011 17:32:03 +0000</pubDate>
		<dc:creator>Mauro</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Auth]]></category>
		<category><![CDATA[django admin]]></category>
		<category><![CDATA[SESSION]]></category>
		<category><![CDATA[subdomain]]></category>

		<guid isPermaLink="false">http://www.mdgart.com/?p=142</guid>
		<description><![CDATA[If you use the Django Auth Framework you may need to know the existence of this constant that you can set in the settings.py of your project:

SESSION_COOKIE_DOMAIN = &#34;.yourdomain.com&#34;

when you login, your cookies session will be set to be valid on every subdomain, so you will be still logged in www.yourdomain.com, yourdomain.com, and any every [...]]]></description>
			<content:encoded><![CDATA[<p>If you use the Django Auth Framework you may need to know the existence of this constant that you can set in the settings.py of your project:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">SESSION_COOKIE_DOMAIN = <span style="color: #483d8b;">&quot;.yourdomain.com&quot;</span></pre></div></div>

<p>when you login, your cookies session will be set to be valid on every subdomain, so you will be still logged in www.yourdomain.com, yourdomain.com, and any every subdomains.yourdomain.com.</p>
<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="mauro@ondadiluce.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy me a coffe for Django Tip: Cross Domain Cookies" /><input type="hidden" name="currency_code" value="EUR" /><input type="hidden" name="amount" value="1" /><input type="image" src="http://www.mdgart.com/wp-content/plugins/buy-me-beer/icon_cafe.gif" align="left" alt="Buy me a coffe" title="Buy me a coffe" hspace="3" style="border:0;" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=mauro@ondadiluce.com&amp;currency_code=EUR&amp;amount=1&amp;return=&amp;item_name=Buy+me+a+coffe+for+Django+Tip:+Cross+Domain+Cookies" target="paypal">Donate 1 euro, buy me a coffee, I need it to write more posts! Thanks ;)</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mdgart.com/2011/01/15/django-tip-cross-domain-cookies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django admin: how to hide fields for certain users (that are not superusers)</title>
		<link>http://www.mdgart.com/2010/04/08/django-admin-how-to-hide-fields-in-a-form-for-certain-users-that-are-not-superusers/</link>
		<comments>http://www.mdgart.com/2010/04/08/django-admin-how-to-hide-fields-in-a-form-for-certain-users-that-are-not-superusers/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 17:56:57 +0000</pubDate>
		<dc:creator>Mauro</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[django admin]]></category>
		<category><![CDATA[ModelAdmin]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">http://www.mdgart.com/?p=102</guid>
		<description><![CDATA[I&#8217;m working on a project and I&#8217;m using the incredible django admin. So, yesterday I needed a way to hide some fields in a model for user that didn&#8217;t have superuser permissions. After some googling, I found a method in the ModelAdmin class that was perfect (well, I think that it&#8217;s perfect) for my needs: [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a project and I&#8217;m using the incredible django admin. So, yesterday I needed a way to hide some fields in a model for user that didn&#8217;t have superuser permissions. After some googling, I found a method in the ModelAdmin class that was perfect (well, I think that it&#8217;s perfect) for my needs: <code><strong>get_form</strong></code>.<br />
The method is not really mentioned in the official django documentation except in the <a href="http://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/#django.contrib.comments.get_form">comment framework</a>, but you can use it in your ModelAdmin subclass as well. It&#8217;s called before the &#8220;change form&#8221; is created, so we can dynamically change it before it&#8217;s displayed.<br />
The principle is very simple: I dynamically populate the <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.exclude">exclude</a> attribute so that if a user is not a superuser I can exclude a field (or more that one field). Let&#8217;s see an example:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> MyModelAdmin<span style="color: black;">&#40;</span>admin.<span style="color: black;">ModelAdmin</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">def</span> get_form<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request, obj=<span style="color: #008000;">None</span>, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">exclude</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>	
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> request.<span style="color: #dc143c;">user</span>.<span style="color: black;">is_superuser</span>:
			<span style="color: #008000;">self</span>.<span style="color: black;">exclude</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'field_to_hide'</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>MyModelAdmin, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">get_form</span><span style="color: black;">&#40;</span>request, obj, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span></pre></div></div>

<p>What it does is simply add the <code><strong>field_to_hide</strong></code> to the <code><strong>exclude</strong></code> list of MyModelAdmin. In this case, the field will be visible only to superusers, checking  the <code><strong>request.user.is_superuser attribute</strong></code>. Pretty simple!</p>
<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="mauro@ondadiluce.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy me a coffe for Django admin: how to hide fields for certain users (that are not superusers)" /><input type="hidden" name="currency_code" value="EUR" /><input type="hidden" name="amount" value="1" /><input type="image" src="http://www.mdgart.com/wp-content/plugins/buy-me-beer/icon_cafe.gif" align="left" alt="Buy me a coffe" title="Buy me a coffe" hspace="3" style="border:0;" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=mauro@ondadiluce.com&amp;currency_code=EUR&amp;amount=1&amp;return=&amp;item_name=Buy+me+a+coffe+for+Django+admin:+how+to+hide+fields+for+certain+users+(that+are+not+superusers)" target="paypal">Donate 1 euro, buy me a coffee, I need it to write more posts! Thanks ;)</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mdgart.com/2010/04/08/django-admin-how-to-hide-fields-in-a-form-for-certain-users-that-are-not-superusers/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Contact me</title>
		<link>http://www.mdgart.com/contact-me/</link>
		<comments>http://www.mdgart.com/contact-me/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 13:38:35 +0000</pubDate>
		<dc:creator>Mauro</dc:creator>
				<category><![CDATA[Art]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mdgart.com/?page_id=55</guid>
		<description><![CDATA[Donate 1 euro, buy me a coffee, I need it to write more posts! Thanks ;)]]></description>
			<content:encoded><![CDATA[[contact-form]
<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="mauro@ondadiluce.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy me a coffe for Contact me" /><input type="hidden" name="currency_code" value="EUR" /><input type="hidden" name="amount" value="1" /><input type="image" src="http://www.mdgart.com/wp-content/plugins/buy-me-beer/icon_cafe.gif" align="left" alt="Buy me a coffe" title="Buy me a coffe" hspace="3" style="border:0;" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=mauro@ondadiluce.com&amp;currency_code=EUR&amp;amount=1&amp;return=&amp;item_name=Buy+me+a+coffe+for+Contact+me" target="paypal">Donate 1 euro, buy me a coffee, I need it to write more posts! Thanks ;)</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mdgart.com/contact-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

