Re: Reverse URL Questions

2008-08-21 Thread Norman Harman

On Thu, Aug 21, 2008 at 1:50 AM, Rodney Topor <[EMAIL PROTECTED]> wrote:
> Second: Doesn't the use of get_absolute_url() somehow violate the
> separation of URLs from models?

I think so.

I have found named urls 
http://www.djangoproject.com/documentation/url_dispatch/#naming-url-patterns 
  url tag in 
templateshttp://www.djangoproject.com/documentation/templates/#url and 
reverse() in views/models/other to be excellent solution and the best 
way to handle this.

Define your urls once in one place, your urls.py file.  Change them and 
magically everything else just works.

-- 
Norman J. Harman Jr.
Senior Web Specialist, Austin American-Statesman
___
It's August!  Keep your cool with the Statesman.  Check out our hot
deals in print and online for back-to-school savings and more!

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Reverse URL Questions

2008-08-21 Thread Rodney Topor



On Aug 21, 5:25 pm, "James Bennett" <[EMAIL PROTECTED]> wrote:
> good stuff

Thanks very much for the good explanation.

> > 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?

Well, perhaps I was unclear because of my limited understanding.  I
now see why I should always define get_absolute_url() in templates,
and that in templates, when I need a URL, I should write {% url
object.get_absolute_url %} or {% url_pattern_name %} (possibly with
extra arguments), right?  Is there a reason to prefer one of these
forms to the other?  They seem interchangable.

And if I need a URL in a view, e.g., in an HttpResponseRedirect()
constructor, I can use the reverse() function, right?

But I still don't understand the permalink decorator is defined the
way it is, so one has to write
get_absolute_url = permalink(get_absolute_url)
Wouldn't it have been simpler to define permalink as an ordinary
function, and write
return permalink('people.views.details', [str(self.id)])
inside the definition of get_absolute_url()?

(Here, the documentation seems to fail to explain why things are as
they are.  Mostly it does a good job of explaining why as well as
what.)
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Reverse URL Questions

2008-08-21 Thread James Bennett

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
-~--~~~~--~~--~--~---



Reverse URL Questions

2008-08-20 Thread Rodney Topor

There's a practical and stylistic issue I don't understand about the
recommended use of Django and I hope someone can enlighten me.
Application design starts with model design and URL structure design.
Each URLconf provides a mapping from URLs to views.   There are
several different mechanisms for providing reverse mappings:
get_absolute_url() methods in models, reverse() function in views, and
{% url ... %} tag in templates.  Maybe others?

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)?

Second: Doesn't the use of get_absolute_url() somehow violate the
separation of URLs from models?

Third: If one is to reverse map views to URLs, which is the best way
to do it (if there is a best way)?

Thanks.
Rodney
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---