Get reverse URL name from full path

时间:2021-05-31 04:37:30

Does anyone knows how I can get the URL name from the request.get_full_path()?

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

For example:

I have this URL in urls.py

我在urls.py中有这个URL

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

In my context_processor.py:

在我的context_processor.py中:

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

How can I return "evaluation" instead of "/evaluation-active/"?

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

Thanks

2 个解决方案

#1


4  

From django docs:

来自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).

Django 1.5中的新功能。 ResolverMatch的一个实例,表示已解析的URL。此属性仅在URL解析发生后设置,这意味着它在所有视图中都可用,但在url解析之前执行的中间件方法中不可用(如process_request,您可以使用process_view)。

There is url_name attribute in ResolverMatch object:

ResolverMatch对象中有url_name属性:

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

For django version < 1.5 there is answer here: How to get the current urlname using Django?

对于django版本<1.5,这里有答案:如何使用Django获取当前urlname?

#2


1  

If using django 1.5 or higher, use request.resolver_match to get the ResolverMatch object.

如果使用django 1.5或更高版本,请使用request.resolver_match获取ResolverMatch对象。

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

if using version before 1.5, then use resolve:

如果使用1.5之前的版本,则使用resolve:

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

#1


4  

From django docs:

来自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).

Django 1.5中的新功能。 ResolverMatch的一个实例,表示已解析的URL。此属性仅在URL解析发生后设置,这意味着它在所有视图中都可用,但在url解析之前执行的中间件方法中不可用(如process_request,您可以使用process_view)。

There is url_name attribute in ResolverMatch object:

ResolverMatch对象中有url_name属性:

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

For django version < 1.5 there is answer here: How to get the current urlname using Django?

对于django版本<1.5,这里有答案:如何使用Django获取当前urlname?

#2


1  

If using django 1.5 or higher, use request.resolver_match to get the ResolverMatch object.

如果使用django 1.5或更高版本,请使用request.resolver_match获取ResolverMatch对象。

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

if using version before 1.5, then use resolve:

如果使用1.5之前的版本,则使用resolve:

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