<?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>Django foo &#187; Arif Harbott</title>
	<atom:link href="http://www.djangofoo.com/author/arif/feed" rel="self" type="application/rss+xml" />
	<link>http://www.djangofoo.com</link>
	<description>Django Tips and Tweaks</description>
	<lastBuildDate>Wed, 18 Aug 2010 09:38:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Session Key Change On Login</title>
		<link>http://www.djangofoo.com/330/session-key-change-on-login</link>
		<comments>http://www.djangofoo.com/330/session-key-change-on-login#comments</comments>
		<pubDate>Sat, 03 Jul 2010 21:05:43 +0000</pubDate>
		<dc:creator>Arif Harbott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[sessionkey]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=330</guid>
		<description><![CDATA[If you use the request.session.session_key for an anonymous user e.g. to store shopping cart information, and then use django.contrib.auth login be aware that the session key will change. This tends to catch out new Django developers.]]></description>
			<content:encoded><![CDATA[<p>If you use the <strong>request.session.session_key</strong> for an anonymous user e.g. to store shopping cart information, and then use <strong>django.contrib.auth login</strong> be aware that the session key will change. This tends to catch out new Django developers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/330/session-key-change-on-login/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing Request Object In Form</title>
		<link>http://www.djangofoo.com/280/accessing-request-object-in-form</link>
		<comments>http://www.djangofoo.com/280/accessing-request-object-in-form#comments</comments>
		<pubDate>Tue, 18 May 2010 19:30:28 +0000</pubDate>
		<dc:creator>Arif Harbott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[request]]></category>
		<category><![CDATA[request object]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=280</guid>
		<description><![CDATA[Something that I use quite regularly is accessing the request object in a form. It is actually pretty simple, in your form you add a custom __init__ function that sets the request as a parameter: class PasswordRecoverForm(PasswordResetForm): def __init__(self, *args, **kwargs): self.request = kwargs.pop('request', None) super(PasswordRecoverForm, self).__init__(*args, **kwargs) def clean_email(self): """Do something with the request""" [...]]]></description>
			<content:encoded><![CDATA[<p>Something that I use quite regularly is accessing the <strong>request object</strong> in a <strong>form</strong>.</p>
<p>It is actually pretty simple, in your form you add a custom __init__ function that sets the request as a parameter:</p>
<pre class="brush:python">class PasswordRecoverForm(PasswordResetForm):
    def __init__(self, *args, **kwargs):
    self.request = kwargs.pop('request', None)
    super(PasswordRecoverForm, self).__init__(*args, **kwargs)
</pre>
<pre class="brush:python">def clean_email(self):
    """Do something with the request"""
    print self.request
</pre>
<p>Then in your view code you can pass the request as a parameter.</p>
<pre class="brush:python">form = PasswordRecoverForm(request=request)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/280/accessing-request-object-in-form/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Change Django Auth Group Ordering</title>
		<link>http://www.djangofoo.com/269/change-django-auth-group-ordering</link>
		<comments>http://www.djangofoo.com/269/change-django-auth-group-ordering#comments</comments>
		<pubDate>Mon, 17 May 2010 19:25:34 +0000</pubDate>
		<dc:creator>Arif Harbott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[auth models]]></category>
		<category><![CDATA[meta]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=269</guid>
		<description><![CDATA[Following on from my last post on changing the behaviour of the Django contrib auth models. Another thing you might like to do is to change the default ordering: from django.contrib.auth.models import User User._meta.ordering = ['-email'] or from django.contrib.auth.models import Group Group._meta.ordering = ['-id']]]></description>
			<content:encoded><![CDATA[<p>Following on from my last post on changing the behaviour of the <strong>Django contrib auth models</strong>. Another thing you might like to do is to change the default ordering:</p>
<pre class="brush:python">from django.contrib.auth.models import User
User._meta.ordering = ['-email']
</pre>
<p>or</p>
<pre class="brush:python">from django.contrib.auth.models import Group
Group._meta.ordering = ['-id']
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/269/change-django-auth-group-ordering/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Change Django Auth User Meta</title>
		<link>http://www.djangofoo.com/271/change-django-auth-user-meta</link>
		<comments>http://www.djangofoo.com/271/change-django-auth-user-meta#comments</comments>
		<pubDate>Thu, 29 Apr 2010 13:26:35 +0000</pubDate>
		<dc:creator>Arif Harbott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[meta]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[models]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=271</guid>
		<description><![CDATA[Sometimes it can be helpful to change the default behaviour of the Auth User model or other models built into Django. One use case might be if you want to change the length of the username field in the Auth User model to allow for longer usernames (e.g. an email address). There is a simple [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it can be helpful to change the default behaviour of the <strong>Auth User model</strong> or other models built into Django.<br />
One use case might be if you want to change the length of the username field in the Auth User model to allow for longer usernames (e.g. an email address).</p>
<p>There is a simple way to change the max length attribute:</p>
<pre class="brush:python">from django.contrib.auth.models import User
User._meta.get_field_by_name('username')[0].max_length=75
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/271/change-django-auth-user-meta/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing Django Decorators</title>
		<link>http://www.djangofoo.com/253/writing-django-decorators</link>
		<comments>http://www.djangofoo.com/253/writing-django-decorators#comments</comments>
		<pubDate>Wed, 14 Apr 2010 15:35:15 +0000</pubDate>
		<dc:creator>Arif Harbott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[custom decorator]]></category>
		<category><![CDATA[decoratorexample]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=253</guid>
		<description><![CDATA[I had a situation today where I had to write a custom decorator for a Django app and thought I would post the code. I first created a file called decorators.py and placed it in the project folder. from django.core.urlresolvers import reverse from django.http import HttpResponse, HttpResponseRedirect from functools import wraps def access_required(permission): def decorator(func): [...]]]></description>
			<content:encoded><![CDATA[<p>I had a situation today where I had to write a custom decorator for a Django app and thought I would post the code.</p>
<p>I first created a file called <strong>decorators.py</strong> and placed it in the project folder.</p>
<pre class="brush:python">from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect

from functools import wraps

def access_required(permission):
    def decorator(func):
        def inner_decorator(request, *args, **kwargs):
            if permission == 'admin':
                if request.session['user_profile'].is_account_admin == 1:
                    return func(request, *args, **kwargs)
                else:
                    return HttpResponseRedirect(reverse('dashboard'))

        return wraps(func)(inner_decorator)

    return decorator
</pre>
<p>Then in my views I simply imported the decorator and added it to my view:</p>
<pre class="brush:python">from webapp.decorators import access_required
@access_required('admin')
    def account_users(request):
        ...
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/253/writing-django-decorators/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Local Development Settings File</title>
		<link>http://www.djangofoo.com/155/local-development-settings-file</link>
		<comments>http://www.djangofoo.com/155/local-development-settings-file#comments</comments>
		<pubDate>Wed, 17 Mar 2010 11:46:07 +0000</pubDate>
		<dc:creator>Arif Harbott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[local settings]]></category>
		<category><![CDATA[settings]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=155</guid>
		<description><![CDATA[If you want to have local development settings in your Django project but don&#8217;t fancy having multiple versions of settings.py then you can use the following snippet at the end of your settings.py file. # Import server specific settings try: from settings_local import * except ImportError: pass Then put any local development settings in a [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to have <strong>local development settings</strong> in your Django project but don&#8217;t fancy having multiple versions of <strong>settings.py</strong> then you can use the following snippet at the end of your <strong>settings.py</strong> file.</p>
<pre class="brush:python"># Import server specific settings
try:
    from settings_local import *
except ImportError:
    pass
</pre>
<p>Then put any local development settings in a file called <strong>settings_local.py</strong> and you can override anything from <strong>settings.py</strong>.<br />
If <strong>settings_local.py</strong> does not exist on your production server no error is thrown.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/155/local-development-settings-file/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django Authentication With Email Instead of Usernames</title>
		<link>http://www.djangofoo.com/154/django-authentication-with-email-instead-of-usernames</link>
		<comments>http://www.djangofoo.com/154/django-authentication-with-email-instead-of-usernames#comments</comments>
		<pubDate>Mon, 15 Mar 2010 10:05:46 +0000</pubDate>
		<dc:creator>Arif Harbott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[auth backends]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[email auth]]></category>
		<category><![CDATA[usernames]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=154</guid>
		<description><![CDATA[A lot of the applications we build use email addresses instead of usernames. Personally I find it a lot easier to remember my email address than an ever growing list of usernames. The way we do this is to write a simple custom auth backend in the following way: 1. In settings.py add the following [...]]]></description>
			<content:encoded><![CDATA[<p>A lot of the applications we build use email addresses instead of usernames. Personally I find it a lot easier to remember my email address than an ever growing list of usernames.</p>
<p>The way we do this is to write a simple custom auth backend in the following way:</p>
<p>1. In <a href="http://docs.djangoproject.com/en/dev/ref/settings/#authentication-backends" target="_blank">settings.py</a> add the following lines (replace yourprojectname with the name of your app):</p>
<pre class="brush:python">AUTHENTICATION_BACKENDS = (
    'yourprojectname.backends.EmailAuthBackEnd',
    'django.contrib.auth.backends.ModelBackend',
)</pre>
<p>2. Create a file called <strong>backends.py</strong> and put it in your root folder (i.e. the same folder as settings.py)</p>
<pre class="brush:python">from django.contrib.auth.backends import ModelBackend
from django.contrib.admin.models import User

class EmailAuthBackEnd(ModelBackend):
    def authenticate(self, email=None, password=None,**kwargs):
        try:
            user = User.objects.get(email=email)  

            if user.check_password(password):
                return user
            return None
        except User.DoesNotExist:
            return None</pre>
<p>3. In your views import the authenticate back end and then call the authenticate function:</p>
<pre class="brush:python">from django.contrib.auth import authenticate
....
user = authenticate(email=email, password=password)</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/154/django-authentication-with-email-instead-of-usernames/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Format DateTime in Django Admin</title>
		<link>http://www.djangofoo.com/129/format-datetime-in-django-admin</link>
		<comments>http://www.djangofoo.com/129/format-datetime-in-django-admin#comments</comments>
		<pubDate>Wed, 10 Mar 2010 20:18:21 +0000</pubDate>
		<dc:creator>Arif Harbott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[format]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=129</guid>
		<description><![CDATA[If you are trying to format a DateTimeField() field in the Django admin you may find it harder than you think. You would think that the DATE_FORMAT and DATETIME_FORMAT in settings.py would apply to the admin BUT it only applies to date formatting in the template. At present there is no easy way to format [...]]]></description>
			<content:encoded><![CDATA[<p>If you are trying to format a <strong>DateTimeField()</strong> field in the Django admin you may find it harder than you think. You would think that the <strong>DATE_FORMAT</strong> and <strong>DATETIME_FORMAT</strong> in settings.py would apply to the admin BUT it only applies to date formatting in the template.</p>
<p>At present there is no easy way to format the date but the following example of one method of how to do it:</p>
<pre class="brush:python">class News(models.Model):
    headline = models.CharField(max_length=100)
    date = models.DateTimeField()
    article = models.TextField()
    published = models.BooleanField()

class NewsAdmin(admin.ModelAdmin):
    list_display = ('headline', 'format_date', 'published')

    def format_date(self, obj):
        return obj.date.strftime('%d %b %Y %H:%M')
    format_date.short_description = 'Date'
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/129/format-datetime-in-django-admin/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Filtering Foreign Key Choices</title>
		<link>http://www.djangofoo.com/121/filtering-foreign-key-choices</link>
		<comments>http://www.djangofoo.com/121/filtering-foreign-key-choices#comments</comments>
		<pubDate>Tue, 09 Mar 2010 10:22:20 +0000</pubDate>
		<dc:creator>Arif Harbott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[filtering]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[limit_choices_to]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=121</guid>
		<description><![CDATA[A common question I get asked is how do you filter the auto generated foreign key select box in the Django. The answer is very easy you use the limit_choices_to method, which can be found on the Model Field reference page on the Django site. Here is an example. You want to limit the author [...]]]></description>
			<content:encoded><![CDATA[<p>A common question I get asked is how do you filter the auto generated foreign key select box in the Django. The answer is very easy you use the <strong>limit_choices_to</strong> method, which can be found on the <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/" target="_blank">Model Field</a> reference page on the Django site.</p>
<p>Here is an example.<br />
You want to limit the author of the news article to only show those users who are in the group admin (which is id: 1). You would use, limit_choices_to={&#8216;groups__in&#8217;: [1]}</p>
<pre class="brush: python">class News(models.Model):
    headline = models.CharField(max_length=100)
    author = models.ForeignKey('users.UserProfile', limit_choices_to={'groups__in': [1]})
    date = models.DateTimeField()
    article = models.TextField()
    published = models.BooleanField()
</pre>
<p>Now when you edit a news article only the users who are in the admin group will be shown.</p>
<p>You can also use a <a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q">Q object</a> instead of a dictionary, which I will cover in another article.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/121/filtering-foreign-key-choices/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

