'"'str' object has no attribute 'tag'" error in Django Tutorial

I am following the Django Tutorial to learn how to work with it, but I have encountered an error very early in it and I'm not sure how to fix it.

It happened while creating the django project and doing the 'Write your first view' section: https://docs.djangoproject.com/en/dev/intro/tutorial01/#write-your-first-view

After following those steps carefully, while executing python3 manage.py runserver the following error appears: AttributeError: 'str' object has no attribute 'tag'

This is the full error trace:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/home/noctis/.local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/noctis/.local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 124, in inner_run
    self.check(display_num_errors=True)
  File "/home/noctis/.local/lib/python3.9/site-packages/django/core/management/base.py", line 438, in check
    all_issues = checks.run_checks(
  File "/home/noctis/.local/lib/python3.9/site-packages/django/core/checks/registry.py", line 77, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/noctis/.local/lib/python3.9/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/noctis/.local/lib/python3.9/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/noctis/.local/lib/python3.9/site-packages/django/urls/resolvers.py", line 448, in check
    for pattern in self.url_patterns:
  File "/home/noctis/.local/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/noctis/.local/lib/python3.9/site-packages/django/urls/resolvers.py", line 634, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/noctis/.local/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/noctis/.local/lib/python3.9/site-packages/django/urls/resolvers.py", line 627, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.9/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 "/home/noctis/code/tests/django/mysite/mysite/urls.py", line 21, in <module>
    path('polls/', include('polls.urls')),
  File "/usr/lib/python3.9/xml/etree/ElementInclude.py", line 128, in include
    _include(elem, loader, base_url, max_depth, set())
  File "/usr/lib/python3.9/xml/etree/ElementInclude.py", line 136, in _include
    if e.tag == XINCLUDE_INCLUDE:
AttributeError: 'str' object has no attribute 'tag'

Maybe there is some incompatibility with the python version and django version? I'm using Python 3.9.7 and Django 4.0.2

Thanks in advance



Solution 1:[1]

So, I've found the mistake I've made. In the tutorial there's a point where you add to mysite/urls.py this snippet:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

The autocomplete feature for python in vscode added a different include than the one found in django.urls. Hence the error.

Solution 2:[2]

Here is the catch, if you will try to use include() without importing it from django.urls your local auto complete will auto import something like this

# from xml.etree.ElementInclude import include

So here remove above and just add,

from django.urls import path, include

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 noctis
Solution 2 Bhushan