'module' object is not iterable when running django website to the server

不想你离开。 提交于 2021-01-04 12:31:45

问题


i wanted to run my django website to their server so i open cmd and go to manage.py directory :

 C:\Users\computer house>cd desktop/newproject 

then i type this code :

python manage.py runserver 

but i got this error :

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03BE5A08>
Traceback (most recent call last):
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py", line 542, in url_patterns
    iter(patterns)
TypeError: 'module' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\urls.py", line 67, in _load_all_namespaces
    namespaces.extend(_load_all_namespaces(pattern, current))
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py", line 549, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'videos.urls' from 'C:\\Users\\computer house\\Desktop\\newproject\\videos\\urls.py'>' 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.

and this is my videso\url.py code :

from django.urls import path
from videos import views

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

what does this error 'module object is not iterable ' mean ?


回答1:


This error seems like either you don't have a /videos/urls.py file or it doesn't contain any valid url patterns.

Step 1. Create a urls.py inside your videos folder(if there is a one, no need to create)
Step 2. add the following code to /videos/urls.py

urlpatterns = []

Empty urlpatterns considered as a valid patter in Django




回答2:


The problem is with spellings urlpatterns = []

and urls.py instead of url.py




回答3:


Two comments:

  1. urlpatternesshould be urlpatterns
  2. In my case, the error was actually in views.py. So the python generated error message was a bit misleading because all of the urls.py files in my project were fine. I was able to get passed this error, once I commented out 90% of my code in views.py (except for a basic index(request) method). Once I had a working homepage, I was able to uncomment my code until I discovered the error.



回答4:


Perhaps useful for someone else who finds this. In my case the error was being caused by an exception further down the import chain, there was nothing wrong with my urls (they hadn't changed since they were last working).

The way to approach an error like this, when the real exception is being masked by something else, is basically to remove all your urls and then add them back in one at a time each time checking whether you get the exception in order to isolate where it's coming from. So start by removing all but one url or url include, check if it runs, if it does then add the next back in and repeat until you get the error.

Then you can apply the same principle to your views, remove all and then re-add until you find the error. At this point you roughly know where your problem is so use the django shell (python manage.py shell) and import the view that you've isolated as having caused the error. This should give you the actual exception which will most likely lead to actually being able to approach the problem.

If just importing the view isn't enough you may want to isolate it into a test case where you can mock a request to give to the view but you shouldn't really need to in cases like this the issue is with the import since you're not actually running the view at the stage of just running your server.




回答5:


If you are a beginner then, deleting the project and recreating it worked for me.



来源:https://stackoverflow.com/questions/51576436/module-object-is-not-iterable-when-running-django-website-to-the-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!