我希望在表单选择字段中获取所有视图函数的名称。
我在控制台中发现了一篇文章,但是它没有在表单中工作。
我稍微更改了代码以满足我的需求。我创建了一个名为get_views_list.py
的文件,并尝试导入all_views_list
from M2.settings import ROOT_URLCONF
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
all_urlpatterns = __import__(ROOT_URLCONF).urls.urlpatterns
detail_views_list = []
def get_all_view_names(urlpatterns):
for pattern in urlpatterns:
if isinstance(pattern, RegexURLResolver):
get_all_view_names(pattern.url_patterns)
elif isinstance(pattern, RegexURLPattern):
detail_views_list.append(pattern.callback.__name__)
get_all_view_names(all_urlpatterns)
all_views_list = []
# remove redundant entries and specific ones we don't care about
for each in detail_views_list:
if each not in "serve add_view change_view changelist_view history_view delete_view RedirectView":
if each not in all_views_list:
all_views_list.append(each)
但这显示错误
Unhandled exception in thread started by <function wrapper at 0x7f93f019cb90>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/niloy/PycharmProjects/M2/M2/urls.py", line 21, in <module>
url(r'^user/', include('UserAccessControl.urls')),
File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/niloy/PycharmProjects/M2/UserAccessControl/urls.py", line 17, in <module>
from .views import *
File "/home/niloy/PycharmProjects/M2/UserAccessControl/views.py", line 13, in <module>
from get_views_list import all_views_list
File "/home/niloy/PycharmProjects/M2/UserAccessControl/get_views_list.py", line 4, in <module>
all_urlpatterns = __import__(ROOT_URLCONF).urls.urlpatterns
AttributeError: 'module' object has no attribute 'urls'