2009/11/24 bcurtu <bcu...@gmail.com>: > No, I don't. > > I want to know the name of the url for a given url pattern. So, when I > get in my method the url someone is asking for (next='/people/'), I > want to know the name of the url that identifies this pattern > (people_name). It's not for a HttpRedirect, it's for statistical > porpouses. > > It's something like resolve, but not reverse. > > Thanks > > On 24 nov, 11:15, rebus_ <r.dav...@gmail.com> wrote: >> 2009/11/24 bcurtu <bcu...@gmail.com>: >> >> >> >> > Hi, >> >> > I want to get the url name from a url. For example, having: >> >> > url( >> > regex = r'^people/$', >> > view = 'people_view', >> > name = 'people_name' >> > ), >> >> > and >> >> > @login_required >> > def people_view(request)... >> > _some_code_here... >> >> > I'm looking for a function that accepts "people/" and returns >> > "people_name". I have tried to use django.core.urlresolvers.resolve, >> > however it returns a wrapper fuction for login_required. >> >> > Is it posible? >> >> > -- >> >> > You received this message because you are subscribed to the Google Groups >> > "Django users" group. >> > To post to this group, send email to django-us...@googlegroups.com. >> > To unsubscribe from this group, send email to >> > django-users+unsubscr...@googlegroups.com. >> > For more options, visit this group >> > athttp://groups.google.com/group/django-users?hl=en. >> >> Usually what you want to do is: >> >> from django.core.urlresolvers import reverse >> reverse('people_name') >> >> http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse >> >> I don't see why you need url name, i think the name is meant to be >> used to resolve urls not other way around. > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > >
How about this? path = request.path url = request.build_absolute_uri(path) resolver = get_resolver(None) view_func, url_args, url_kwargs = resolver.resolve(path) sigs = resolver.reverse_dict.getlist(view_func) url_name = None # Loop through all the items in the reverse dictionary. for key, value in resolver.reverse_dict.items(): # Check if the value of the mapping is one of our matching signatures and # that the key is a string. if value in sigs and type(key) == str: try: # See if we have the right parameters to use this reversal and that # it produces the correct url. if resolver.reverse(key, *url_args, **url_kwargs) == path[1:]: # No exceptions were thrown so we have the right parameters and # the path matched therefore we've found the url name we were # seeking - which of course means we can stop looking. url_name = key break except NoReverseMatch, e: # The parameters were wrong - ah well, maybe the next one will # succeed. pass print url, url_name, url_kwargs This is taken from the greatlemers-django-tools [1]. It seems like lots of code, guess you could use it as middleware or context_processor. It should return something like: http://127.0.0.1:8000/home/ home {} where "home" is url name [1] http://code.google.com/p/greatlemers-django-tools/source/browse/trunk/gdt_nav/models.py#158 Hope this helps Davor -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.