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

Posted on

Another error involving the execution of a Django-based web application is in the content of this article. This article will discuss about how to solve the error message above. The error appear in the complete output as follows :

(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/settings.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 'todoapp' is not defined

Apparently, the above error is actually giving complete information to solve the problem itself. Actually, python cannot solve the keyword ‘todoapp’. So, it need to be in a definition so that python will recognize and execute it. But the context of the problem actually exist in the following line as it appears in the error message ‘/home/user/python/django/todoproject/todoproject/urls.py’. The name of the app itself in this article’s context is ‘todoapp’. The following is the line causing the error message to appear :

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

In the file with the name of ‘urls.py’ where it is a file for specifying an additional url, it suddenly trigger an error. The additional url is imported from the application with the name of ‘todoapp’. The main intention is just to include a file with the name of ‘urls.py’ exist in the application folder with the name of ‘todoapp’. But python does not recognize this intention. Therefore, there is a need to modify the above line so that python will accept the import process of ‘urls.py’ in the folder todoapp. The solution is simple, just change the above line into the following one :

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

Make sure and do not forget to check whether the file with the name of ‘urls.py’ exist in the folder of ‘todoapp’. This article is part of the trial or experiment to simulate the lesson in the article with the title of ‘How to Build Todo App with Django’ in this link.

Leave a Reply