2

I've the following URL that i created:

urls.py

url(r'^authors', GenericView.as_view(model=Author, context_object_name='authors_list',
     success_url=reverse_lazy('author_link'),
     template_name='app/authors.html'), name='author_url_name'),

And i want to have access to the URL's name on the view, passed in a variable. In this case, 'author_url_name'.

The view function where i need it, is the following:

views.py

def get_context_data(self, **kwargs):
    context = super(AuthorClass, self).get_context_data(**kwargs)
    context['action'] = reverse_lazy('author_url_name')
    return context

This works, but i want to replace the 'author_url_name' for some method that gives me exactly that.

Thanks for your time!!

Community
  • 1
  • 1
Nunovsky
  • 365
  • 4
  • 18

1 Answers1

3

https://stackoverflow.com/a/17614086/183948 has your answer for you.

As of Django 1.5, this can be accessed from the request object

current_url = request.resolver_match.url_name

https://docs.djangoproject.com/en/1.5/ref/request-response/#django.http.HttpRequest.resolver_matc

Community
  • 1
  • 1
Brad Beattie
  • 579
  • 3
  • 13
  • Thanks Brad for your answer. But, the class where i use the function get_context_data(self, **kwargs) is a CreateView, and it's not passed a 'request'. So, when i do `context['action'] = reverse_lazy(request.resolver_match.url_name)`, it doesn't recognize the `request`bit. How do i solve this? Sorry, i'm a newbie. – Nunovsky Jul 30 '14 at 18:30
  • 3
    You can get it from `self.request`. – Daniel Roseman Jul 30 '14 at 18:43