is a django application with some url helper functionality.
Currently it provides a template tag to generate a project specific url/permalink for a model. This can be used instead of the models get_absolute_url
method when reusing django apps whose the get_absolute_url
method don't generate the urls you would like to use in your project.
A little Example using the excellent Coltrane blog application. Coltranes get_absolute_url generates the following URLs /2009/Feb/10/the_slug. The month name is abbreviated. I wanted my permalinks to use the months number i.e. /2009/02/10/the_slug.
My first solution was the included template tag url
, which looked something like this:
{% url blog_entry_detail object.pub_date|date:"Y" object.pub_date|date:"m" object.pub_date|date:"d" object.slug %}
I don't think thats very nice. So I came up with the get_permalink template tag.
Let's get started.
-
Activate the app by adding url_utils to your INSTALLED_APPS.
-
Create an entry in your urls.py to suit your needs.
Mine looks like:
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', views.object_detail, dict(entry_info_dict, month_format='%m', slug_field='slug'), name = 'blog_entry_detail'),
-
Create a permalinks.py file in your projects root dir or somewhere on your python path.
-
The permalinks module needs to contain a class which has the same name as the application whose model we want to create permalinks for. We then need to create one classmethod for each model we create permalinks for. The method name needs to be get_<MODELNAME>_url. The method also needs one parameter the object is will create the permalink for. All names are lower cased.
from django.core.urlresolvers import reverse class coltrane(object): @classmethod def get_entry_url(cls, entry): return reverse( 'blog_entry_detail', kwargs = { 'year': entry.pub_date.strftime('%Y'), 'month': entry.pub_date.strftime('%m'), 'day': entry.pub_date.strftime('%d'), 'slug': entry.slug })
-
Now update the templates. Load the template library and use the new tags.
{% load url_utils_tags %} {% get_permalink entry%}
-
That's it