LoginSignup
kenken0413
@kenken0413

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

django [module 'first_app.urls' from '/Users/ken/python/first_project/first_app/urls.py'>' does not appear to have any patterns in it.]のエラーについて

Q&AClosed

解決したいこと

djangoのviewを使った画面の表示エラーについて

発生している問題・エラー

Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/urls/resolvers.py", line 717, in url_patterns
    iter(patterns)
TypeError: 'module' object is not iterable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/management/commands/runserver.py", line 133, in inner_run
    self.check(display_num_errors=True)
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/management/base.py", line 485, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/checks/urls.py", line 42, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/checks/urls.py", line 72, in _load_all_namespaces
    namespaces.extend(_load_all_namespaces(pattern, current))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/checks/urls.py", line 61, in _load_all_namespaces
    url_patterns = getattr(resolver, "url_patterns", [])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "/Users/ken/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/urls/resolvers.py", line 725, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'first_app.urls' from '/Users/ken/python/first_project/first_app/urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.

該当するソースコード

urls.py(first_app)

from django.urls import path
from . import views

app_name = 'first_app'

urlpatterns = [
    path('hello', views.index, name='index'),
]

view.py(first_app)

def greet
  puts Hello World
end

setting.py(first_project)

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

urls.py(first_project)

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

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

自分で試したこと

一度削除し、再度作成
各記述の間違いの確認
不要な項目のコメントアウト

同種事例について調べたところ、ほとんどが記述ミスでした。

エラー文の最後が、
' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.

となっているので、urls.pyで使用している'first_app.urls'がない、といった内容と思われるので、呼び出した時の記述がどこかおかしいと思っているのですが、解決に至りませんでした。

0

1Answer

urls.py(first_app)
from . import views

urlpatterns = [
    path('hello', views.index, name='index'),
]

urls.pyの上記箇所から、view.py内はindexという関数を持っていることになっています。
ですが、記載のview.pyにはgreet関数のみが存在し、しかもpythonの構文と異なります。
下記のようにview.pyを編集してみると如何でしょうか。

view.py
from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world.")
2Like

Comments

  1. @kenken0413

    Questioner

    ありがとうございます、解決いたしました!

Your answer might help someone💌