Session Key Change On Login

Posted: July 3rd, 2010 | Author: Arif Harbott | Filed under: Django | Tags: , | No Comments »

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.


Accessing Request Object In Form

Posted: May 18th, 2010 | Author: Arif Harbott | Filed under: Django | Tags: , , | No Comments »

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"""
    print self.request

Then in your view code you can pass the request as a parameter.

form = PasswordRecoverForm(request=request)

Change Django Auth Group Ordering

Posted: May 17th, 2010 | Author: Arif Harbott | Filed under: Django | Tags: , | No Comments »

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']

Change Django Auth User Meta

Posted: April 29th, 2010 | Author: Arif Harbott | Filed under: Django | Tags: , , | No Comments »

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 way to change the max length attribute:

from django.contrib.auth.models import User
User._meta.get_field_by_name('username')[0].max_length=75

Writing Django Decorators

Posted: April 14th, 2010 | Author: Arif Harbott | Filed under: Django | Tags: , | 1 Comment »

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):
        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

Then in my views I simply imported the decorator and added it to my view:

from webapp.decorators import access_required
@access_required('admin')
    def account_users(request):
        ...