Django singularize filter

To make a new custom filter I created a templatetags/ directory in my app directory. After that I added an empty __init__.py file and a template file with the following content:

from django import template
register = template.Library()

import re

SINGULARS= [
 (r's$', ''),
 (r'(n)ews$', '\1ews'),
 (r'([ti])a$', '\1um'),
 (r'((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$', '\1\2sis'),
 (r'(^analy)ses$', '\1sis'),
 (r'([^f])ves$', '\1fe'),
 (r'(hive)s$', '\1'),
 (r'(tive)s$', '\1'),
 (r'([lr])ves$', '\1f'),
 (r'([^aeiouy]|qu)ies$', '\1y'),
 (r'(s)eries$', '\1eries'),
 (r'(m)ovies$', '\1ovie'),
 (r'(x|ch|ss|sh)es$', '\1'),
 (r'([m|l])ice$', '\1ouse'),
 (r'(bus)es$', '\1'),
 (r'(o)es$', '\1'),
 (r'(shoe)s$', '\1'),
 (r'(cris|ax|test)es$', '\1is'),
 (r'(octop|vir)i$', '\1us'),
 (r'(alias|status)es$', '\1'),
 (r'^(ox)en', '\1'),
 (r'(vert|ind)ices$', '\1ex'),
 (r'(matr)ices$', '\1ix'),
 (r'(quiz)zes$', '\1'),
 ]

@register.filter
def singularize(word):
 for pattern,replacement in SINGULARS:
   word, n =  re.subn(pattern, replacement, word)
   if n>0:
     break
 return word

The regular expressions I took from Ruby on Rails.

Customizing Django admin interface

When extending the admin interface the two most important fields are the list_display field and the search_fields.
Set search_fields to enable a search box on the admin change list page. This should be set to a list of field names. If you want to add a field which is ForeignKey you should spelify field name using double underscore.
Set list_display to control which fields are displayed on the relevant admin page. If the field is ForeignKey it displays the __unicode__() of the related object.
In my project I wanted to display user’s first name, last name and username. So in my models.py I added to my UserProfile class a method user_fields which returns everything I need:

def user_fields(self):
 return self.user.first_name + ' ' + self.user.last_name + ' ('  + self.user.username + ')'

and in my admin.py:

class UserProfileAdmin(admin.ModelAdmin):
 list_display = ('user_fields', 'hair','eyes', 'address', 'telephone', 'birth_date',  'group', 'second_language')
search_fields = ('user__first_name', 'user__last_name', 'user__username','hair','eyes',)
admin.site.register(UserProfile, UserProfileAdmin)

Django and flash

For implementing flash on your web page Django’s middleware classes are verry useful. I used the function process_response(), but about that – later.
First for implementing flash on my web page, I used a variable, which i set in the session and gave it different values. May be it will become more clear with an example:

 request.session['flash'] = u"Some text which will appear as flash"

After that in the base template I added the variable {{ request.session.flash }} and displayed it only if it was set:

{% if request.session.flash %}
{{ request.session.flash }}
{% endif %}

Finally, I wrote a middleware class to delete the flash variable after it has been displayed once:

class FlashMiddleware:
def process_response(self, request, response):
   if hasattr(request, 'session') and 'flash' in request.session and not isinstance(response, H    ttpResponseRedirect): del request.session['flash']
return response

The function process_response(self, request, response) must return an HttpResponse object.
More about Django middleware classes you can find here.

After that i just added my middleware class in the MIDDLEWARE_CLASSES in my settings.py file:

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'middleware.FlashMiddleware'
)

Django and unicode

In the beginning of the file views.py just add the following:

# -*- coding: utf-8 -*-