Web Images Maps News Groups Books Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Reverse URL lookup implementation
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Expand all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Adrian Holovaty  
View profile  
 More options Apr 7 2006, 8:24 am
From: "Adrian Holovaty" <holov...@gmail.com>
Date: Thu, 6 Apr 2006 15:24:14 -0500
Local: Fri, Apr 7 2006 8:24 am
Subject: Reverse URL lookup implementation

On the plane from London to Chicago yesterday, I implemented something
that's been discussed on this list lately -- "reverse" URL creation.

I've attached a new urlresolvers.py to this e-mail. It's intended to
replace the one in django/core, and it works with magic-removal only.
(To use it with trunk, just replace the import line at the top to
import Http404 from django.core.exceptions instead of django.http.)
I've also attached unit tests.

Given a view name (e.g. 'myproject.polls.views.poll_detail', as a
string) and any number of positional or keyword arguments, this code
generates the URL according to the first URL pattern in your URLconf
that matches.

The method is reverse() on both the RegexURLPattern and
RegexURLResolver classes.

Here's an example of how it works. Given this root URLconf:

urlpatterns = patterns('',
    (r'^foo/$', 'path.to.foo_view'),
    (r'^dates/(\d{4})/(\w{3})/$', 'path.to.month_view'),
    (r'^people/(?P<state>\w\w)/(?P<name>\w+)/$', 'path.to.person_view'),
)

Here's how to use it:

>>> from django.conf import settings
>>> from django.core import urlresolvers
>>> resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF)
>>> resolver.reverse('path.to.foo_view')
'foo/'
>>> resolver.reverse('path.to.month_view', '2005', 'apr')
'dates/2005/apr/'
>>> resolver.reverse('path.to.person_view', state='il', name='adrian')
'people/il/adrian/'
>>> resolver.reverse('path.to.person_view', 'il', 'adrian')

'people/il/adrian/'
# In the following, 'invalidstate' fails the regex test (it isn't two
characters long).
>>> resolver.reverse('path.to.person_view', 'invalidstate', 'adrian')

Traceback (most recent call last):
...
NoReverseMatch

Ideally there'd be a template-tag interface to this. Something like:

    {% link 'path.to.month_view' 2005 'apr' %}
    {% link 'path.to.person_view' state='il' name='adrian' %}

It's implemented by analyzing the regular expression, which was quite
fun. This means it'll probably break for wacky regexes, but I think a
95% solution is fine.

Thoughts?

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

  urlresolvers.py
7K Download

  reverse_unit_tests.py
2K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ivan Sagalaev  
View profile  
 More options Apr 7 2006, 8:38 am
From: Ivan Sagalaev <Man...@SoftwareManiacs.Org>
Date: Fri, 07 Apr 2006 00:38:43 +0400
Local: Fri, Apr 7 2006 8:38 am
Subject: Re: Reverse URL lookup implementation

Adrian Holovaty wrote:
>Ideally there'd be a template-tag interface to this. Something like:

>    {% link 'path.to.month_view' 2005 'apr' %}
>    {% link 'path.to.person_view' state='il' name='adrian' %}

It's unbelievable :-). I was thinking about it just 15 minutes ago but
only with 'url' instead of 'link' :-). And without that much details...

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Kaplan-Moss  
View profile  
 More options Apr 7 2006, 8:40 am
From: Jacob Kaplan-Moss <ja...@jacobian.org>
Date: Thu, 6 Apr 2006 15:40:40 -0500
Local: Fri, Apr 7 2006 8:40 am
Subject: Re: Reverse URL lookup implementation
On Apr 6, 2006, at 3:24 PM, Adrian Holovaty wrote:

> On the plane from London to Chicago yesterday, I implemented something
> that's been discussed on this list lately -- "reverse" URL creation.

Wow.

This is frickin' amazing, Adrian; it's exactly what I kept trying to  
come up with and couldn't think all the way through.

/me has another "I wish I'd thought of that" moment... :)

Jacob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Kaplan-Moss  
View profile  
 More options Apr 7 2006, 8:47 am
From: Jacob Kaplan-Moss <ja...@jacobian.org>
Date: Thu, 6 Apr 2006 15:47:16 -0500
Local: Fri, Apr 7 2006 8:47 am
Subject: Re: Reverse URL lookup implementation
I just had another thought on this:

On Apr 6, 2006, at 3:24 PM, Adrian Holovaty wrote:

> Ideally there'd be a template-tag interface to this. Something like:

>     {% link 'path.to.month_view' 2005 'apr' %}
>     {% link 'path.to.person_view' state='il' name='adrian' %}

One thing we could do -- to make this even easier -- would be to grab  
data from an object passed into the {% link %} tag.  So in your  
example urlconf::

        (r'^people/(?P<state>\w\w)/(?P<name>\w+)/$', 'path.to.person_view'),

If a Person object had ``state`` and ``name`` attributes, you could do::

        {% link path.to.person_view person %}

(where ``some_person`` is an object in the context) as a shortcut to::

        {% link path.to.person_view state=person.state name=person.name %}

That way if you set up your URLconfs in a logical manner -- I'd bet  
90% of my urlconfs already work this way -- the shortcut version  
would Just Work™.

Jacob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Max Battcher  
View profile  
 More options Apr 7 2006, 9:21 am
From: "Max Battcher" <m...@worldmaker.net>
Date: Thu, 6 Apr 2006 17:21:39 -0400
Local: Fri, Apr 7 2006 9:21 am
Subject: Re: Reverse URL lookup implementation
On 4/6/06, Jacob Kaplan-Moss <ja...@jacobian.org> wrote:

> That way if you set up your URLconfs in a logical manner -- I'd bet
> 90% of my urlconfs already work this way -- the shortcut version
> would Just Work™.

Unfortunately the Generic Views don't fit this too well...
'object_id' is not 'id'.

--
--Max Battcher--
http://www.worldmaker.net/
All progress is based upon a universal innate desire on the part of
every organism to live beyond its income. --Samuel Butler


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
limodou  
View profile  
 More options Apr 7 2006, 12:55 pm
From: limodou <limo...@gmail.com>
Date: Fri, 7 Apr 2006 08:55:36 +0800
Local: Fri, Apr 7 2006 12:55 pm
Subject: Re: Reverse URL lookup implementation
On 4/7/06, Adrian Holovaty <holov...@gmail.com> wrote:

Great! But whether the syntax can be more easier when we used it in
views or other source code? I think the tag used in template is simple
enough. Maybe be there should be an easy helper function like `link`
to do that. Just like:

>>> from django.core.urlresolvers import link
>>> link('path.to.month_view', '2005', 'apr')

'dates/2005/apr/'

I think it's not difficult.

+1

--
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adrian Holovaty  
View profile  
 More options Apr 7 2006, 1:48 pm
From: "Adrian Holovaty" <holov...@gmail.com>
Date: Thu, 6 Apr 2006 20:48:31 -0500
Local: Fri, Apr 7 2006 1:48 pm
Subject: Re: Reverse URL lookup implementation
On 4/6/06, limodou <limo...@gmail.com> wrote:

> Great! But whether the syntax can be more easier when we used it in
> views or other source code? I think the tag used in template is simple
> enough. Maybe be there should be an easy helper function like `link`
> to do that. Just like:

> >>> from django.core.urlresolvers import link
> >>> link('path.to.month_view', '2005', 'apr')
> 'dates/2005/apr/'

Yes, we'd expose a simpler API to this -- the example I gave in my
e-mail was low-level.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eugene Lazutkin  
View profile  
 More options Apr 7 2006, 2:49 pm
From: Eugene Lazutkin <eugene.lazut...@gmail.com>
Date: Thu, 06 Apr 2006 21:49:37 -0500
Local: Fri, Apr 7 2006 2:49 pm
Subject: Re: Reverse URL lookup implementation
Finally! :-)

Adrian Holovaty wrote:
> On the plane from London to Chicago yesterday, I implemented something
> that's been discussed on this list lately -- "reverse" URL creation.

On conceptual level it looks similar to what I started to implement for
my site with a small insignificant difference --- your code actually
works and mine doesn't. :D

> Ideally there'd be a template-tag interface to this. Something like:

>     {% link 'path.to.month_view' 2005 'apr' %}
>     {% link 'path.to.person_view' state='il' name='adrian' %}

And this is the best part. It didn't occur to me to use template tags to
utilize the reverse lookup, and I didn't even plan for it. Wow! Thank
you for impressing me once again!

As soon as it matures let's create a repository for reusable Django apps.

Thanks,

Eugene


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
limodou  
View profile  
 More options Apr 7 2006, 3:03 pm
From: limodou <limo...@gmail.com>
Date: Fri, 7 Apr 2006 11:03:54 +0800
Local: Fri, Apr 7 2006 3:03 pm
Subject: Re: Reverse URL lookup implementation
On 4/7/06, Eugene Lazutkin <eugene.lazut...@gmail.com> wrote:

Wow, another amazing idea.

> Thanks,

> Eugene

--
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eugene Lazutkin  
View profile  
 More options Apr 7 2006, 3:31 pm
From: Eugene Lazutkin <eugene.lazut...@gmail.com>
Date: Thu, 06 Apr 2006 22:31:58 -0500
Local: Fri, Apr 7 2006 3:31 pm
Subject: Re: Reverse URL lookup implementation

limodou wrote:

>> As soon as it matures let's create a repository for reusable Django apps.

> Wow, another amazing idea.

This is actually how it all started. Many people in Django community
implemented really good applications (hugo, sune, limodou, ...) but in
order to reuse them we have to modify their code customizing them for
our web sites. For example on my web site there are private apps, which
reuse some of my blog components. The easiest way to reuse them was ...
to copy them to a different location with slight modifications, so they
will not clash with the existing instances and produce different URLs. I
don't think this is a proper solution.

While many problems can be solved with existing Django facilities + a
healthy dose of discipline and conventions (e.g., use externally
specified db_table, so a user can assign different tables for different
instances of a model), the reverse URL lookup required a support from
Django. It looks like we have it now. If we resolve the missing parts,
we can have reusable applications, which can be easily installed and
integrated in Django-powered web sites.

What is missing? The convention for database table names assignment is
missing of course. Anything else? i18n-related parameters? The way to
specify custom parameters? How to instantiate several instances of the
same app? How to package an app? Can we reuse Python Eggs for that? Can
Paste help us? I have more questions than answers, but now I can see the
light at the end of the tunnel.

Thanks,

Eugene


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
medhat  
View profile  
 More options May 3 2006, 4:51 am
From: "medhat" <medhat.ass...@gmail.com>
Date: Tue, 02 May 2006 09:51:15 -0700
Local: Wed, May 3 2006 4:51 am
Subject: Re: Reverse URL lookup implementation

Hi,

Is there any reason this is not added to the repository?

--
Thanks!
Medhat


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adrian Holovaty  
View profile  
 More options May 3 2006, 5:06 am
From: "Adrian Holovaty" <holov...@gmail.com>
Date: Tue, 2 May 2006 12:06:52 -0500
Local: Wed, May 3 2006 5:06 am
Subject: Re: Reverse URL lookup implementation
On 5/2/06, medhat <medhat.ass...@gmail.com> wrote:

> Is there any reason this is not added to the repository?

We did a feature-freeze for magic-removal for a while, but, now that
it's been merged, I can get back to work on this.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SmileyChris  
View profile  
 More options May 16 2006, 4:31 pm
From: "SmileyChris" <smileych...@gmail.com>
Date: Mon, 15 May 2006 21:31:09 -0700
Local: Tues, May 16 2006 4:31 pm
Subject: Re: Reverse URL lookup implementation
I look forwards to the addition of this...

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google