Django: Get relative uri from views.py

泪湿孤枕 提交于 2021-02-20 04:49:05

问题


Here is what I have currently:

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

I would like to extract the relative uri from the view. I could just use regex, but I think there is already something out there that would let me do this.


回答1:


This will give you "/myapp/this/is/relative":

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

Link to Django docs page: https://docs.djangoproject.com/en/dev/ref/urlresolvers/



来源:https://stackoverflow.com/questions/25675366/django-get-relative-uri-from-views-py

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!