python - 从完整路径获取反向 URL 名称

标签 python django

有谁知道如何从 request.get_full_path() 获取 URL 名称?

例如:

我在 urls.py 中有这个 URL

url(r'^evaluation-active/$', 'web.evaluation.evaluation', name='evaluation'),

在我的 context_processor.py 中:

def is_evaluation(request):
    return {"test":request.get_full_path()}

如何返回“evaluation”而不是“/evaluation-active/”?

谢谢

最佳答案

来自 django docs :

HttpRequest.resolver_match

New in Django 1.5. An instance of ResolverMatch representing the resolved url. This attribute is only set after url resolving took place, which means it’s available in all views but not in middleware methods which are executed before url resolving takes place (like process_request, you can use process_view instead).

ResolverMatch 对象中有url_name 属性:

def is_evaluation(request):
    return {"test": request.resolver_match.url_name}

对于 django 版本 < 1.5,这里有答案:How to get the current urlname using Django?

关于python - 从完整路径获取反向 URL 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22115547/

相关文章:

python - 满足我的应用程序脚本需求的最小 Python 构建?

python - Pyspark - 计算每个数据框列中的空值数

python - 如何使用 python-pptx 更改形状内的字体颜色?

python - django查询中的模数

django - Apache mod_wsgi django启用多线程多进程

javascript - Jquery 无法从 Django 生成的元素中获取 ID

python - 使用 django 表单处理图像

Python:pyswip 输出返回 Atom 和 Functor

Python 面向对象设计;返回、设置实例变量或两者

python - Django channel 和 azure