On Fri, 2009-01-16 at 14:02 +1100, Ian Cullinan wrote: > Thanks for the explanation, but I still don't understand why it > doesn't work with function references. > > For one, > http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse says > "viewname is either the function name (either a function reference, or > the string version of the name, if you used that form in > urlpatterns)",
Hmm .. you're right it does say that. Okay, it didn't just work by accident, then. But it's still the import name problem and that's a Python issue, hence probably best to avoid it. > and the underlying dict for reverse() uses the function references as > keys wether you call by string or function reference, right? Do get > the same callable no matter how you import the function or does this > not tell the whole story: > > >>> import facesearch.views > >>> from facesearch.views import start_search > >>> facesearch.views.start_search == start_search > True > > You're probably right that using "name=..." is the best solution, but > it seems inelegant to me - for the case of one urlpattern per view, a > reference to that view should be a perfectly unambiguous way to > specify which urlpattern you want. Should be... possibly. Simply isn't true, though, in Python. If you import the same module as "foo.bar" in one place and "bar" in another place (if your Python path permits that), they will be different objects. Their "names" will be different as well. So if you can guarantee that everything is always imported via the same path, including when Django imports it for URLConf purposes, then you're fine. That's why function references are legal. But it's not always easy to guarantee or debug that, hence the unambiguous "name" form existing. Function references in URLConf patterns pre-date reversing, but are very useful for error checking (you know immediately that you imported the wrong thing). For reversing purposes they can be fragile. Life's like that sometimes. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] 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 -~----------~----~----~----~------~----~------~--~---

