"包括"很奇怪

Rah*_*ali 2 python django django-urls

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

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

下面是这给出错误的时候.

urlpatterns = [
    url(r'^home$', include(acc_views.home), name='accounts_home'),
]
Run Code Online (Sandbox Code Playgroud)

这是一个例外:

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.
Run Code Online (Sandbox Code Playgroud)

什么是包括实际上在做什么?

Say*_*yse 5

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

url(r'^home$', include(acc_views.home), name='accounts_home'),
Run Code Online (Sandbox Code Playgroud)

应该

 url(r'^account/', include(account.urls, namespace='accounts'),
Run Code Online (Sandbox Code Playgroud)

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

实际上包括什么?

您可以在此处查看源代码

它主要查找urlpatterns变量内定义的模式.