Django:从views.py获取相对uri

时间:2014-09-04 21:38:01

标签: django

以下是我目前的情况:

urls.py:

 ...
url(r'this/is/relative', 'myapp.views.callview', name='myapp_callview'),
...

views.py:

def callview(request, **kwargs):

    # I can get the complete url by doing this
    print request.build.absolute_uri() # Prints: https://domain:8080/myapp/this/is/relative

    # How do I just get: /myapp/this/is/relative or even /this/is/relative

我想从视图中提取相对uri。我可以使用正则表达式,但我认为已经有一些东西可以让我这样做。

1 个答案:

答案 0 :(得分:2)

这将为您提供"/myapp/this/is/relative"

from django.core import urlresolvers
relative_uri = urlresolvers.reverse("myapp_callview")

链接到Django文档页面:https://docs.djangoproject.com/en/dev/ref/urlresolvers/