Re: How to resolve the view name from a url
Note that url pattern's aren't required to have names, so you can't capture statistics on unnamed urls patterns in apps that you want to use unmodified. If, on the other hand, you just want to collect statistics on url patters which are "under your control", and thus can be required to have names, you can add a copy of the name (or whatever token you like) to the extra context. You could even make it less painful by providing your own implementation of url that does it for you, either with a different name, which you apply to the patterns of interest, or to the whole file by importing your version as url. You must be sure that all the views in question are prepared for the extra context. Bill On Tue, Nov 24, 2009 at 5:48 AM, bcurtu wrote: > 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_ wrote: >> 2009/11/24 bcurtu : >> >> >> >> > 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. > > > -- 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.
Re: How to resolve the view name from a url
> > 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_ wrote: > >> 2009/11/24 bcurtu : > > >> > 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 > > athttp://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... > > Hope this helps > Davor It would have made things a whole lot easier if the resolver would return the url name, but that piece of code should do what you're after. Since all you get back is a view function the second part that iterates over the reverse_dict is necessary to ensure that you retrieve the name of the actual reversed url and not an alternative one that uses the same view (probably with different parameters). -- 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.
Re: How to resolve the view name from a url
2009/11/24 bcurtu : > 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_ wrote: >> 2009/11/24 bcurtu : >> >> >> >> > 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.
Re: How to resolve the view name from a url
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_ wrote: > 2009/11/24 bcurtu : > > > > > 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.
Re: How to resolve the view name from a url
2009/11/24 bcurtu : > 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 at > http://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 to resolve the view name from a url
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 at http://groups.google.com/group/django-users?hl=en.