简体   繁体   中英

The included URLconf 'myapp.urls' does not appear to have any patterns in it

I have an interesting issue.. hope you have seen this kind of behavior.

I have django 3.1.4, running on python 3.6.2 and MySQL as the backend db. The project I am working on works fine, i can access all the urls and the endpoints function like they way they should.

While working on the code, when I make a mistake in one of the classmethod of a class or any utility method called by the views.py file and try to run django, python manage.py runserver localhost:4444 , I get this error, 'The included URL.conf 'myapp.urls' does not appear to have any patterns in it.` I know that I didnt touch the urls file and everything is how it should be (besides the error in my classmethod).

If I run, python manage.py makemigrations , only then can i see the actual error that needs to be resolved. Otherwise, only error that django throws when running the runserver is the URL.conf error.

Besides the imports, I only have this block in my myapp.urls . I need these for the @api_view s that I have in my views.py file.

urlpatterns = [
    url('admin/', admin.site.urls),
    url('api/', include('other.urls')),
    url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]

# and the other.urls is, besides the imports
# used for @api_view
urlpatterns = [
    url('monitor', views.monitor, name='monitor'),
]

# used for Views based on ModelViewSet
router = routers.DefaultRouter()
router.register(r'path', views.MyViewSet)
urlpatterns += router.urls

I have tested with admin and static lines commented out but still the same situation.

For the sake of completeness, here is the error thats thrown. Issue itself doesnt really have anything to do with URL.conf though.

Traceback (most recent call last):
  File "C:\~Path-to-app~\venv\lib\site-packages\django\urls\resolvers.py", line 591, 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:\Program Files\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Program Files\Python\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\~Path-to-app~\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\~Path-to-app~\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
    self.check(display_num_errors=True)
  File "C:\~Path-to-app~\venv\lib\site-packages\django\core\management\base.py", line 396, in check
    databases=databases,
  File "C:\~Path-to-app~\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\~Path-to-app~\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\~Path-to-app~\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\~Path-to-app~\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
    for pattern in self.url_patterns:
  File "C:\~Path-to-app~\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\~Path-to-app~\venv\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'cap_api.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.

Additional Details on how I reproduce it. When everything is working fine, break the indentation in one of the methods that are called by the api_view/method. run runserver command to get the error.

#views.py
@api_view
def monitor(request):
  utils.test_method(request)
  return Response(data="service is up", status=status.HTTP_200_OK)

# utils
def test_method(request):
   #... no code in it.. just a declaration of the method.

Anything I need to configure or is it by design? If you need more information, happy to provide that.

I remember that this error occures if during the init phase of your app some code is executed with a relation to urls and the urls are not yet setup due to the sequence of the init steps that django takes through your app. For example if an indentation mistake leeds to a line of code not being anymore inside a function def, this line of code is suddenly executed in an early stage of the init and if it needs urls it may be the reason for the error. This is why you get the error without touching the urls.py.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM