有谁知道如何从 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/