Django:从views.py获取相对uri [英] Django: Get relative uri from views.py

查看:43
本文介绍了Django:从views.py获取相对uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前拥有的:

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.我可以只使用正则表达式,但是我认为已经有一些东西可以让我做到这一点.

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.

推荐答案

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

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

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

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

这篇关于Django:从views.py获取相对uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆