You could define url patterns for the universal object_type instead of individually for artists, albums, and songs:
urlpatterns = patterns('tlkmusic.apps.tlkmusic_base.views', # (r'^$', index), url(r'^(?P<object_type>\w+)/$', music_object_list, name='music_object_list'), url(r'^(?P<object_type>\w+)/(?P<starts_with>\w)/$', music_object_list, name='music_object_list_x'), url(r'^(?P<object_type>\w+)/(?P<object_id>\d+)/$', music_object_detail, name='music_object_detail'), )
Then in your template the URL tag will be
{% url music_object_list_x object_type starts_with %} *
You may think that you only need one view, music_object_list . If you find that you need different functions for each type of object, then call the individual functions in music_object_list .
def music_object_list(request, object_type, starts_with=None): if object_type == 'artists': return artist_list(request, starts_with=starts_with) elif object_type == 'albums': return album_list(request, starts_with=starts_with) ...
If you are using django.views.generic.list_detail.object_list , be sure to add object_type to the object_type dictionary. This will add object_type to the template context, allowing the url tag to work.
extra_context = {'object_type': 'songs', ...}
* This is the new url tag syntax for Django 1.2. For older versions, you should use a comma.
{% url music_object_list_x object_type,starts_with %}
See documents ( Current , 1.1 ) for more information.