On Thu, Aug 21, 2008 at 1:50 AM, Rodney Topor <[EMAIL PROTECTED]> wrote: > So: Why is the use of explicit URLs discouraged, especially in > templates? Why is it better to write {% url > project_name.app_name.views.results %} in a template than to write / > results/ (assuming the URLconf maps /results/ to the view results)?
So say you write a blog application. It has an Entry model, and its get_absolute_url() method looks like this: def get_absolute_url(self): return "/weblog/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d"), self.slug) All is well and good. Then you need to roll out the same app, on the same server, but for a different site. And that site doesn't want its weblog entries living at "/weblog/"; it wants, maybe, "/articles/". Oops. Oh, and it turns out that's going to conflict with your news application, which is already trying to use "/articles/" in its get_absolute_url(). The news stories are going to have to move to "/stories/". And instead of having photographs at "/pics/", this site needs to have them at "/photos/". And the list goes on. So you can go mess around with ABSOLUTE_URL_OVERRIDES to try to make this all work, which is a bit of a nasty hack and requires you to be constantly duplicating information: not only do you have to lay out the URL patterns in urls.py, you also have to go hard-code the *same* sets of URLs in ABSOLUTE_URL_OVERRIDES. This gets very ugly, very quickly. Trust me, because I've been in that exact situation and I know from experience that it does not end well. Or you can just write the get_absolute_url() method to use reverse resolution so that the only thing you have to do is write the URL patterns you'd be writing anyway, and the reverse resolution will make sure get_absolute_url() returns the right URL. > Second: Doesn't the use of get_absolute_url() somehow violate the > separation of URLs from models? Nope. In object-oriented programming an object is *supposed* to know things about itself. You're supposed to be able to take an object and ask it questions like "who created you?" and "when were you last updated?" and, yes, even "where can I see you on the public web site?" Setting up your code so that questions about a model object can be answered using nothing more than that model object's own API drastically improves the cleanliness of your code; you've got one place to go for these questions, instead of fifteen. > Third: If one is to reverse map views to URLs, which is the best way > to do it (if there is a best way)? Clarify what you mean by this? -- "Bureaucrat Conrad, you are technically correct -- the best kind of correct." --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---