如何修复以下有关获取 URL 的错误



我无法访问(获取(程序中提到的那些网址

urls.py

from django.conf.urls import include, url
from rest_framework import routers
from imgstore.views import QueryImage
from imgstore.views import ImageActionViewSet

# this is DRF router for REST API viewsets
router = routers.DefaultRouter()
router.register(r'api/v1/imgaction', ImageActionViewSet, r"imgaction")
router.register(r'api/v1/queryimage', QueryImage, r"queryimage")

urlpatterns = [
    url(r'', include(router.urls, namespace='imgaction')),
    url(r'', include(router.urls, namespace='queryimage'))
]

我收到此错误:

Error occured while running server:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/root/.pyenv/versions/3.5.7/lib/python3.5/site-packages/django/urls/resolvers.py", line 581, 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 "/root/.pyenv/versions/3.5.7/lib/python3.5/threading.py", line 914, in _bootstrap_innerself.run()
File "/root/.pyenv/versions/3.5.7/lib/python3.5/threading.py", line 862, in runself._target(*self._args, **self._kwargs)
File "/root/.pyenv/versions/3.5.7/lib/python3.5/site-packages/django/utils/autoreload.py", line 54, in wrapperfn(*args, **kwargs)
File "/root/.pyenv/versions/3.5.7/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 117, in inner_runself.check(display_num_errors=True)
File "/root/.pyenv/versions/3.5.7/lib/python3.5/site-packages/django/core/management/base.py", line 390, in check include_deployment_checks=include_deployment_checks,
File "/root/.pyenv/versions/3.5.7/lib/python3.5/site-packages/django/core/management/base.py", line 377, in _run_checksreturn checks.run_checks(**kwargs)
File "/root/.pyenv/versions/3.5.7/lib/python3.5/site-packages/django/core/checks/registry.py", line 72, in run_checksnew_errors = check(app_configs=app_configs)
File "/root/.pyenv/versions/3.5.7/lib/python3.5/site-packages/django/core/checks/urls.py", line 13, in check_url_config return check_resolver(resolver)
File "/root/.pyenv/versions/3.5.7/lib/python3.5/site-packages/django/core/checks/urls.py", line 23, in check_resolverreturn check_method()
File"/root/.pyenv/versions/3.5.7/lib/python3.5/sitepackages/django/urls/resolvers.py", line 398, in checkfor pattern in self.url_patterns:
File "/root/.pyenv/versions/3.5.7/lib/python3.5/site-packages/django/utils/functional.py", line 80, in __get__res = instance.__dict__[self.name] = self.func(instance)
File "/root/.pyenv/versions/3.5.7/lib/python3.5/site-packages/django/urls/resolvers.py", line 588, in url_patterns raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'image_storage.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.

而且我不知道要解决它。我错过了什么?

检查 django 应用程序中可能存在的文件 urls.py image_storage。它应该包含一个有效的 django url 配置,而不是空!

很多时候,这是由 Django 的 resolvers.py 试图消耗你的 urls.py 时的一些异常引起的。

我通常解决它的方法是将我的整个 urls.py 文件(以及所有其他 urls.py 文件(包装在一个 try-except 块中,并打印被捕获的异常,这通常比 Django 提供的默认文件更直接地解决问题。

祝你好运!

最新更新