How to Solve Error Message ‘NameError: name ‘include’ is not defined when executing python manage.py runserver

Posted on

This is another article where the focus is to solve an error message occur when executing the command of ‘python manage.py runserver’. Well, it is a command to run a django application. Before the error occur, when the internal webserver for executing Django-based application is running, there is a source code modification. Editing the file with the name of ‘urls.py’ cause the following error appear :

(env) user@hostname:~/python/django/todoproject$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
March 15, 2020 - 02:37:45
Django version 2.2.6, using settings 'todoproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
...
/home/user/python/django/todoproject/todoproject/urls.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/user/anaconda3/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/home/user/anaconda3/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/urls/resolvers.py", line 399, in check
    for pattern in self.url_patterns:
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/urls/resolvers.py", line 584, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/user/python/django/env/lib/python3.7/site-packages/django/urls/resolvers.py", line 577, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/user/python/django/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 1006, in _gcd_import
  File "", line 983, in _find_and_load
  File "", line 967, in _find_and_load_unlocked
  File "", line 677, in _load_unlocked
  File "", line 728, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "/home/user/python/django/todoproject/todoproject/urls.py", line 21, in 
    path('',include(todoapp.urls)),
NameError: name 'include' is not defined

Actually, according to the above output command execution, the error is ‘NameError: name ‘include’ is not defined. This is an effect of defining or importing a Django-based application URL. This is the line of code specifying the import process :

   path('',include(todoapp.urls)),

It means, include the file with the name of ‘urls.py’ in the folder todoapp. But apparently, after finishing to define the above line, the error message of ‘NameError: name ‘include’ is not defined’ appear. Well, this is actully python doesn’t recognize the keyword of ‘include’. So, the solution is quite simple. Actually, ‘include’ is part of the library exist in Django. Therefore, just add the following line to import the library with the keyword of ‘include’. Just add the following line :

from django.urls import path,include

Finally, that step above will solve the error. As an additional information, the above source code is part of the lesson or article exist in this link with the title of ‘How to Build a Todo App with Django’.

Leave a Reply