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

Change root password

Posted: April 18th, 2010 | Author: Davo | Filed under: Django | Tags: | No Comments »

The manage.py provides a changepassword option which allows you to change the password of any given User (django.contrib.auth).

To change the password of the root account:

python manage.py changepassword
Changing password for user 'root'
Password: xxx
Password (again): xxx
Password changed successfully for user 'root'

If you want to change the password for another User:

python manage.py changepassword someuser
Error: user 'someuser' does not exist

UUID

Posted: April 16th, 2010 | Author: Davo | Filed under: Django | Tags: | No Comments »

Generating UUID(A Universally Unique IDentifier) example: (only from python 2.5+)

>>> import uuid
>>> unique_id = str(uuid.uuid4())
>>> print unique_id
e31c4c33-2d02-49ef-a501-a9f947f86697
>>>

Reference: http://docs.python.org/library/uuid.html


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

Sending HTML email

Posted: April 12th, 2010 | Author: Davo | Filed under: Django | Tags: , | 3 Comments »

It is possible to send a HTML email using the EmailMultiAlternatives

Example:

from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags

subject, from_email, to = 'Order Confirmation', 'admin@yourdomain.com', 'someone@somewhere.com'

html_content = render_to_string('the_template.html', {'varname':'value'}) # ...
text_content = strip_tags(html_content) # this strips the html, so people will have the text as well.

# create the email, and attach the HTML version as well.
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()