简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM