gpt4 book ai didi

python - "Include"工作异常

转载 作者:行者123 更新时间:2023-11-28 20:40:16 25 4
gpt4 key购买 nike

我最近开始为 python 开发 Django 1.9。我也是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'),
]

异常(exception)情况:

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.

include 实际上在做什么?

最佳答案

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

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

应该是

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

Include 旨在简化链接不同 urls.py 文件之间的模式,而不是包含单独的 View ,为此您可以像往常一样直接在 url 中引用 View 。

What is include actually doing?

您可以查看 source code here

它本质上是寻找在 urlpatterns 变量中定义的模式。

关于python - "Include"工作异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36181044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com