运行Django应用遇URL配置错误,请求技术排查帮助
用Django部署机器学习模型时runserver报错:URLconf无有效路由规则
环境配置:
- Python版本:
python 3.6 - Django版本:
django==2.2.4
操作过程:
跟着教程部署机器学习模型,执行到第3步时自行编写代码无法运行,遂复制教程内完整Django代码,执行python manage.py runserver命令时触发以下错误:
(venv) C:\Users\SEYDOU GORO\my_ml_service2\backend\server>python manage.py runserver Watching for file changes with StatReloader Performing system checks... Exception in thread django-main-thread: Traceback (most recent call last): File "C:\Users\SEYDOU GORO\my_ml_service2\venv\lib\site-packages\django\urls\resolvers.py", line 604, 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 "C:\Users\SEYDOU GORO\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\SEYDOU GORO\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "C:\Users\SEYDOU GORO\my_ml_service2\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper fn(*args, **kwargs) File "C:\Users\SEYDOU GORO\my_ml_service2\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run self.check(display_num_errors=True) File "C:\Users\SEYDOU GORO\my_ml_service2\venv\lib\site-packages\django\core\management\base.py", line 423, in check databases=databases, File "C:\Users\SEYDOU GORO\my_ml_service2\venv\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks new_errors = check(app_configs=app_configs, databases=databases) File "C:\Users\SEYDOU GORO\my_ml_service2\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config return check_resolver(resolver) File "C:\Users\SEYDOU GORO\my_ml_service2\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver return check_method() File "C:\Users\SEYDOU GORO\my_ml_service2\venv\lib\site-packages\django\urls\resolvers.py", line 416, in check for pattern in self.url_patterns: File "C:\Users\SEYDOU GORO\my_ml_service2\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "C:\Users\SEYDOU GORO\my_ml_service2\venv\lib\site-packages\django\urls\resolvers.py", line 611, in url_patterns raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e django.core.exceptions.ImproperlyConfigured: The included URLconf 'server.urls' 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.
问题分析与解决方案
错误核心是TypeError: 'module' object is not iterable,说明根URL配置(server/urls.py)中直接引用了整个urls模块,而非模块内的urlpatterns列表,或未正确使用include()函数。
修正根URL配置
打开server/urls.py,将错误的引用方式:
from django.contrib import admin from django.urls import path, include from ml_service import urls urlpatterns = [ path('admin/', admin.site.urls), path('', urls), ]
修改为以下两种正确方式之一:
方式1:使用include()引用app的urls模块
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('ml_service.urls')), ]
方式2:直接导入app的urlpatterns
from django.contrib import admin from django.urls import path from ml_service.urls import urlpatterns as ml_urls urlpatterns = [ path('admin/', admin.site.urls), path('', ml_urls), ]
验证app的URL配置
同时检查ml_service/urls.py是否正确定义了urlpatterns列表,示例如下:
from django.urls import path from . import views urlpatterns = [ path('predict/', views.predict, name='predict'), # 其他需要的路由规则 ]
排查循环导入(可选)
若上述修改后仍报错,检查是否存在循环导入:比如views.py中导入了某个模块,而该模块又间接导入urls.py相关内容,需调整导入逻辑。
内容的提问来源于stack exchange,提问作者Seydou GORO
相关产品推荐
相关产品推荐

