5

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

For example:

I have this URL in urls.py

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

In my context_processor.py:

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

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

Thanks

Marcos Aguayo
  • 6,840
  • 8
  • 28
  • 61

2 Answers2

4

From 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).

There is url_name attribute in ResolverMatch object:

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?

Community
  • 1
  • 1
ndpu
  • 22,225
  • 6
  • 54
  • 69
  • Thanks, and you know how to redirect in the context_processor.py? It doesn't work – Marcos Aguayo Mar 01 '14 at 14:42
  • @MarcosAguayo hm, im dont know, why you want to redirect in context processor instead of view? – ndpu Mar 01 '14 at 14:50
  • I want to check if user had approve the evaluation, if not redirect to the page. I have 20 pages, I'm trying not to repeat the code. – Marcos Aguayo Mar 01 '14 at 14:52
  • 1
    @MarcosAguayo example of redirect middleware: https://github.com/redsolution/django-redirect-middleware/blob/master/redirects/middleware.py – ndpu Mar 01 '14 at 15:00
1

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

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

if using version before 1.5, then use resolve:

def is_evaluation(request):
    return {"test":resolve(request.path_info).url_name}
almalki
  • 4,595
  • 26
  • 30