在Django中,如何自检应用程序URL? [英] In Django, how do I introspect application urls?

查看:75
本文介绍了在Django中,如何自检应用程序URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在瓶子或烧瓶中,我可以执行以下操作:

In bottle or flask I can do something like this:

for route in app.routes:
    description = route.callback.__doc__
    method = method

所以我可以遍历所有网址(路由)注册给定的应用程序,并查看其回调函数(以Django行话查看),接受的方法,等等。

So I can loop through all url (routes) register for given application and see its callback function (view in Django jargon), accepted methods and so on.

我想知道django是否可以实现相同功能。因此,我想获取给定应用程序的所有URL以及链接到这些URL的所有视图。

I'm wondering if the same is possible with django. So I would like to get all urls for given app and all views that are linked to those urls. Is that can be done?

推荐答案

您需要导入 urls 模块来自项目。 urls.urlpatterns 是您所需要的。尝试以下功能:

You need to import urls module from project. urls.urlpatterns is what you are looking for. Try this function:

import urls # or from app import urls
def print_urls(patterns, indent=0):
    for u in patterns:
        if u.callback:
            print '-'*indent, u.regex.pattern, u.callback
        else:
            print '='*(indent+1), u.regex.pattern
            print_urls(u.url_patterns, indent+3)

print_urls(urls.urlpatterns)

这篇关于在Django中,如何自检应用程序URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆