orokusaki
orokusaki

Reputation: 57138

In Django, how can I use a request to determine its URLconf viewname?

I can get the view function from request.path:

from django.core.urlresolvers import resolve
view_func, _args, _kwargs = resolve(request.path)

However, I need something more. I need to take a list of view names, like ['edit_foo', 'delete_foo'], and find out if the current URL is for one of those.

I've come up with a couple ideas using some internals from django.core.urlresolvers, but I want something that will be efficient and somewhat correct (ie, not hacky, ideally documented).

Upvotes: 2

Views: 1014

Answers (1)

orokusaki
orokusaki

Reputation: 57138

After writing that long question, I figured it out :/ (posting for whoever else runs into this, by chance). It's quite simple:

>>> resolve(request.path).url_name
'edit_foo'

I must have been mistaken about the resolve function's usefulness, which is vast.

Upvotes: 5

Related Questions