How to fix ''module' object is not iterable' error, in Python with Django project

Viewed 11415

I am trying to create an webpage using Python and Django. I have just created a simple template and tried to run the sever but I get errors and I'm not able to understand..

I check all the spelling in my projects and searched the similar issues, however I am not able to solve it.

settings.py

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # My apps
    'learning_logs',
    ]

urls.py

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

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

views.py

from django.shortcuts import render

# Create your views here.
def index(request):
    """The home page for Learning Log"""
    return render(request, 'learning_logs/index.html')

urls.py // This is second URL

"""Defines url patterns for learning_logs."""

from django.urls import path
from . import views

app_name = 'learning_logs'
urlpatterns = [
    # Home page.
    path('', views.index, name='index'),
    ]

Error on command prompt when I tried to run server:

"C:\Users\User\Desktop\python_work\project_3\learning_log\11_env\lib\site-packages\django\urls\resolvers.py", line 535, in url_patterns
    iter(patterns)
TypeError: 'module' object is not iterable

During handling of the above exception, another exception occurred:

.....

"C:\Users\User\Desktop\python_work\project_3\learning_log\11_env\lib\site-packages\django\urls\resolvers.py", line 542, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'learning_logs.urls' (namespace)>'
  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.
3 Answers

You have created two paths in both urls.py for the / page.

As I know this is not the correct way.

You should make one path to /. And make the second path by giving any path name like

path(learn/'', include('learning_logs.urls')

In your project urls.py, try changing like:

from django.urls import path, re_path, include
urlpatterns = [
   path('admin/', admin.site.urls),
   re_path(r'^', include('learning_logs.urls')),
]

If you see valid patterns in the file then the issue is probably caused by a circular import.

So first you must check all installed package is installed in vurtual environment.
If you are in doubt,
Let's remove everything except from setuptools, pkg_resources, pip from site-packages.

and then Install again in a virtual environment

$ python3 -m pip install -r requirements.txt

Related