LoginSignup
2
1

More than 1 year has passed since last update.

Django で、URLリゾルバのビュー名を列挙するコード

Last updated at Posted at 2016-09-21

Django の URL 解析機 (URLリゾルバ) で使うURL名 ( reverse() の第一引数に入れるやつ )の一覧を表示するコード

from django.urls.resolvers import get_resolver
root_resolver = get_resolver()

def _join_namespace(a, b):
    return (a + ':' if a else '') + (b or '')

def _print_resolver(namespace, resolver):
    _namespace = _join_namespace(namespace, resolver.namespace)
    for ns, (prefix, res) in resolver.namespace_dict.items():
        _print_resolver(_namespace, res)
    for n, l in resolver.reverse_dict.items():
        if callable(n):
            continue
        print(_join_namespace(_namespace, n))

_print_resolver('', root_resolver)

結果

admin:blog_blogpost_delete
admin:blog_blogpost_add
admin:auth_user_changelist
admin:galleries_gallery_delete
admin:form_lottery_electedformentry_delete
admin:contact_contact_changelist
admin:redirects_redirect_add
admin:datastructure_datastructurepage_changelist
admin:forms_form_changelist
admin:pages_richtextpage_change
admin:pages_link_change
admin:blog_blogpost_change
admin:datastructure_datastructure_changelist
admin:datastructure_datastructure_history
admin:news_newscategory_delete
admin:auth_group_delete
admin:blog_blogcategory_history
admin:pages_richtextpage_changelist
admin:auth_user_add
admin:sites_site_change
admin:sites_site_changelist
admin:forms_form_delete
admin:form_lottery_electedformentry_changelist
admin:login
admin:galleries_gallery_add
admin:news_newsarticle_add
admin:conf_setting_change
admin:pages_page_change
admin:pages_page_changelist
admin:sites_site_delete
admin:redirects_redirect_history
admin:galleries_gallery_change
admin:blog_blogcategory_add
...
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1