I recently started development on Django 1.9 for python. I am a newbie in python as well. Just working through examples and codes to learn stuff. I came across include in django.conf.urls which when I used caused errors. I couldn't understand why was that? Because I have used it at other places which don't cause errors.
from django.conf.urls import url, include
from accounts import views as acc_views
urlpatterns = [
url(r'^home$', acc_views.home, name='accounts_home'),
]
Below is when this gives error.
urlpatterns = [
url(r'^home$', include(acc_views.home), name='accounts_home'),
]
Here is the 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.
What is include actually doing?
You appear to be including a view rather than a urls module
url(r'^home$', include(acc_views.home), name='accounts_home'),
should be
url(r'^account/', include(account.urls, namespace='accounts'),
Include is designed to make it easy to link the patterns between different urls.py files, not to include a separate view, to do that you can just reference the view directly in a url as you would do normally.
What is include actually doing?
You can view the source code here
It essentially looks for patterns defined inside a urlpatterns variable.
include works by including other django module's urls and use current definition as root below other ones.
Django doc has very comprehensive explanation about this, I'm going to quote them here:
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)), ]
Here the include(extra_patterns) would use credit/ as the root url and recognize any other urls defined in extra_patterns as an extension of the url definition to match urls. This avoids duplicate definitions like credit/reports, credit/charge, etc.
Same thing for include('apps.help.urls'), it would include all urls defined in module apps.help.urls with base url as help/. So you don't have to define all urls in one place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With