Problems with Urls in Django

Asked

Viewed 486 times

0

Expensive,

I’m with the following mistake my project:

django.core.exceptions.ImproperlyConfigured: The included URLconf
'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.

This project worked properly, but I had to format my machine. After downloading the files from my git again, it started giving this problem.

Follow my api.url:

from django.contrib import admin
from django.urls import include, path
from rest_framework import routers
from ChatBot.views import UserViewSet
from ChatBot.views import GroupViewSet
from django.conf.urls import url

router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.

urlpatterns = [
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('admin/', admin.site.urls),
    url(r'^', include('ChatBot.urls')),
]

My Chatbot.urls:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^resultado/$', views.ClassRankingView.as_view(), name='resultado'),
    url(r'^learning/$', views.WordEmbeddingCalculation.as_view(), name='learning'),
    url(r'^weight/$', views.NovaViewTeste.as_view(), name='weight'),
]

The complete error:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/willian/ChatBot/venv/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 "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/home/willian/ChatBot/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/home/willian/ChatBot/venv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/home/willian/ChatBot/venv/lib/python3.5/site-packages/django/core/management/base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/willian/ChatBot/venv/lib/python3.5/site-packages/django/core/management/base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/willian/ChatBot/venv/lib/python3.5/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/willian/ChatBot/venv/lib/python3.5/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/home/willian/ChatBot/venv/lib/python3.5/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/home/willian/ChatBot/venv/lib/python3.5/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/willian/ChatBot/venv/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 '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.

This same code works on other machines, but not on mine. They could tell me what might be happening?

  • You quote api.url and api.urls at different times. It wouldn’t just be a typo/nomenclature?

1 answer

0

Try the following:

from django.contrib import admin
from django.urls import include, path
from rest_framework import routers
from ChatBot.views import UserViewSet
from ChatBot.views import GroupViewSet
#from django.conf.urls import url

router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.

urlpatterns = [
    path('', include('router.urls')),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('admin/', admin.site.urls),
    path('', include('ChatBot.urls', namespace='chatbot')),
]

Chatbot.urls:

from django.conf.urls import url
from . import views


app_name = 'chatbot'


urlpatterns = [
    path('resultado/', views.ClassRankingView.as_view(), name='resultado'),
    path('learning/', views.WordEmbeddingCalculation.as_view(), name='learning'),
    path('weight/', views.NovaViewTeste.as_view(), name='weight'),
]

I believe that using path solves the problem and "namespace" can help when referencing urls in the template, for example, you can reference the "Weight" url using {% 'chatbot:Weight url' %}.

  • Thank you so much for your help. I tested what you sent, but you keep making the mistake. What I did to solve, was to clean my urls and take the ones I wasn’t using, leaving only the ones that were actually going to be used. And even then, sometimes the error still occurs. I must have set something wrong on my machine.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.