On 3/27/06, Doug Van Horn <[EMAIL PROTECTED]> wrote:
> In thinking about the url definitions in urls.py, it occurs to me that
> a major rewrite of the url patterns could prove to be a pain.  For
> example, I have an app where the urls are deployed under /foo/, and I
> have a URL defined as '^bar/\d+/$'.
>
> When drawing a link to that view in HTML from another app I would 'hard
> code' that URL as, say, 'href="/foo/bar/99"'.

Hey Doug,

The convention is to put the URL-creation logic in your models, in a
get_absolute_url() method. Here's a quick example:

    class Person(meta.Model):
        gender = meta.CharField(maxlength=1) # 'm' or 'f'
        full_name = meta.CharField(maxlength=50)

    def get_absolute_url(self):
        return '/people/%s/%s/' % (self.gender,
self.full_name.lower().replace(' ', ''))

You're right to imply that this goes against the DRY principle,
because you have to define URLs in two places -- the URLconfs and the
models. In practice, this isn't that big of a deal (in my humble
opinion), but we've given some thought to putting the
get_absolute_url() logic in URLconfs instead, so at least the logic is
in a single file.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to