开发者

Django 'resolve' : get the url name instead of the view_function

My problem is simple,开发者_开发问答 I have an url, I would like to resolve it, but get the url name instead of the view function associated with it ...

For example... this is urlconf :

urlpatterns = patterns('',
...
    url('^/books/$', book_list, name="overview_books"),
...
)

And this is what I would like :

>>> resolve('/books/')
'overview_books'

Do you know any way to do this ?


In Django 1.3 and newer you can use resolve function:

from django.core.urlresolvers import resolve
print resolve('/books/').url_name


Use this snippet (originally taken from djangosnippets.org/snippets/1378/);

>>> from my_projects.tools import resolve_to_name
>>> print resolve_to_name('/some/url')
'app.views.view'
>>> print resolve_to_name('/some/other/url')
'this_is_a_named_view'

;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜