在 Django 中,如何使用请求来确定其 URLconf 视图名称?
我可以从 request.path 获取视图函数:
from django.core.urlresolvers import resolve
view_func, _args, _kwargs = resolve(request.path)
但是,我还需要更多东西。我需要获取视图名称列表,例如 ['edit_foo', 'delete_foo']
,并查明当前 URL 是否属于其中一个。
我使用 django.core.urlresolvers 的一些内部机制提出了一些想法,但我想要一些高效且正确的东西(即,不是 hacky,理想的文档记录)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
写完那个长问题后,我想通了:/(为其他偶然遇到这个问题的人发帖)。这很简单:
我一定是错误地理解了
resolve
函数的用处,它的用处是巨大的。After writing that long question, I figured it out :/ (posting for whoever else runs into this, by chance). It's quite simple:
I must have been mistaken about the
resolve
function's usefulness, which is vast.