How to Solve Error Message TypeError: view must be a callable or a list/tuple in the case of include().

Posted on

Introduction

This is an article which is focusing on how to solve a specific error message. That specific error exist in the title of this article. It is ‘TypeError: view must be a callable or a list/tuple in the case of include()’. Actually, after editing an ‘urls.py’, it is getting out of control and displaying an error without any display in the page. The error appear as a log appearance in the running internal service. By running ‘python manage.py runserver’, it will execute an internal service which is going to retrieve request. But as soon as the access to a certain URL as a request, it triggers error as follows :

C:\python\programming\myproject\hello\views.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\app\python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\app\python39\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\python\programming\myproject\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\python\programming\myproject\env\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\python\programming\myproject\env\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "C:\python\programming\myproject\env\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\python\programming\myproject\env\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\python\programming\myproject\env\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\python\programming\myproject\env\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "C:\python\programming\myproject\env\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\python\programming\myproject\env\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\python\programming\myproject\env\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\python\programming\myproject\env\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\app\python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\python\programming\myproject\myproject\urls.py", line 21, in <module>
path('hello/', include('hello.urls')),
File "C:\python\programming\myproject\env\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\app\python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\python\programming\myproject\hello\urls.py", line 10, in <module>
path('home-url', 'views.home_function', name='home_url_name'),
File "C:\python\programming\myproject\env\lib\site-packages\django\urls\conf.py", line 73, in _path
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().

Solution

After trying to identify the message which exist in the error message above, there are several specific lines which is hinting the cause of the error. The following are those lines :

path('home-url', 'views.home_function', name='home_url_name'),
File "C:\python\programming\myproject\env\lib\site-packages\django\urls\conf.py", line 73, in _path
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().

Suddenly, the cause for triggering the error message is found. It is a mistake which is causing the error. The format of the URL definition is wrong. It is informing that there is something with the view definition. The message is that the view must be a callable or a list/tuple in the case of include(). It obvious since defining the views with the function associated does not use single quote in it. Comparing to other definition format of the URL, just correct the definition of the URL access. From the following one :

path('home-url', 'views.home_function', name='home_url_name'),

Into the following one :

path('home-url', views.home_function, name='home_url_name'),

The target function in the views does not need any quotes in it to be defined properly. Access the URL again to display the page. If there are no further error appear, the page will display any returned content of the file defined in the function of ‘home_function’.


Leave a Reply