“包括”很奇怪

时间:2016-03-23 14:32:59

标签: python django django-urls

我最近开始使用Django 1.9 for python进行开发。我也是python的新手。只需通过示例和代码来学习东西。 我在 django.conf.urls 中遇到了 include ,当我使用时会导致错误。我不明白为什么会这样?因为我在其他不会导致错误的地方使用过它。

from django.conf.urls import url, include
from accounts import views as acc_views
urlpatterns = [
    url(r'^home$', acc_views.home, name='accounts_home'),
]

下面是错误的时候。

urlpatterns = [
    url(r'^home$', include(acc_views.home), name='accounts_home'),
]

以下是例外:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x035424F8>
Traceback (most recent call last):
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\urlresolvers.py", line 419, in url_patterns
    iter(patterns)
TypeError: 'function' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\management\commands\runserver.py", line 116, in inner_run
    self.check(display_num_errors=True)
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\management\base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\checks\urls.py", line 27, in check_resolver
    warnings.extend(check_resolver(pattern))
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\checks\urls.py", line 27, in check_resolver
    warnings.extend(check_resolver(pattern))
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\urlresolvers.py", line 426, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf '<function home at 0x03D45A50>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

包含实际上在做什么?

3 个答案:

答案 0 :(得分:5)

您似乎包含view而不是urls模块

url(r'^home$', include(acc_views.home), name='accounts_home'),

应该是

 url(r'^account/', include(account.urls, namespace='accounts'),

Include旨在让您轻松链接不同urls.py文件之间的模式,而不是包含单独的视图,为此您可以直接在网址中引用视图,就像您通常那样。

  

实际包含的内容是什么?

您可以查看source code here

它主要查找urlpatterns变量中定义的模式。

答案 1 :(得分:2)

include的工作原理是包含其他django模块的url,并使用当前定义作为root以下的其他目录。

Django doc对此有非常全面的解释,我将在这里引用它们:

from django.conf.urls import include, url

from apps.main import views as main_views
from credit import views as credit_views

extra_patterns = [
    url(r'^reports/$', credit_views.report),
    url(r'^reports/(?P<id>[0-9]+)/$', credit_views.report),
    url(r'^charge/$', credit_views.charge),
]

urlpatterns = [
    url(r'^$', main_views.homepage),
    url(r'^help/', include('apps.help.urls')),
    url(r'^credit/', include(extra_patterns)),
]

此处include(extra_patterns)会使用credit/作为根网址,并将extra_patterns中定义的任何其他网址识别为网址定义的扩展名,以匹配网址。这样可以避免重复定义,例如credit/reportscredit/charge等。

include('apps.help.urls')同样如此,它将包含模块apps.help.urls中定义的所有网址,基本网址为help/。因此,您不必在一个地方定义所有网址。

答案 2 :(得分:0)

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x035424F8>
Traceback (most recent call last):
  File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\urlresolvers.py", line 419, in url_patterns
    iter(patterns)
TypeError: 'function' object is not iterable

告诉你应该知道的一切。函数不可迭代。如果您read the docs,您会发现可以使用以下任何一种:

include(module, namespace=None, app_name=None)[source]
include(pattern_list)
include((pattern_list, app_namespace), namespace=None)
include((pattern_list, app_namespace, instance_namespace))

我猜你用acc_views.home传递了一个函数。你可能想要{em}字符串 'acc_views.home'或其他东西,而不是实际的模块。