Scone
Scone

Reputation: 669

Django URLs randomly shows ImproperlyConfigured

I sometimes receive an ImproperlyConfigured "The included URLconf 'app_name.urls' does not appear to have any patterns in it." on my website. When I revisit the URL that caused the error, it works fine. The error shows that "patterns" is a module in these cases. Most of the time it properly loads my URLs.

Does anyone spot a problem with my URLs? This is a strange issue that occurs on both the root URL (/) and the details page (####/details). I've never seen an error for my other URLs, but they don't receive much traffic. I haven't been able to reproduce the error, but I receive it several times a day.

I'm using Python 3.5.2 with Django 2.0. The codebase was original written with Django 1.6 I believe. I recently migrated to 2.0 and that is when I noticed the issue.

I modified django/urls/resolvers.py to log the 'patterns' variable and most of the time I receive this list:

[<URLResolver <URLPattern list> (admin:admin) 'admin/'>,
 <URLPattern '<int:pk>/details' [name='details']>,
 <URLPattern 'check/' [name='check']>,
 <URLPattern 'logout/' [name='logout']>,
 <URLPattern 'search/' [name='search']>,
 <URLPattern 'features/' [name='features']>,
 <URLPattern 'terms-of-service/' [name='terms']>,
 <URLPattern 'privacy-policy/' [name='privacy']>,
 <URLPattern '' [name='index']>]

urls.py

from django.urls import path, re_path
from django.contrib import admin

from app_name import views
from app_name.result_view import SubmissionDetailView



handler404 = 'app_name.views.not_found_view'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('<int:pk>/details', SubmissionDetailView.as_view(), name='details'),
    path('check/', views.check, name='check'),
    path('logout/', views.logout_view, name='logout'),
    path('search/', views.search, name='search'),
    path('features/', views.features, name='features'),
    path('terms-of-service/', views.terms, name='terms'),
    path('privacy-policy/', views.privacy, name='privacy'),
    path('', views.index, name='index'),
]

Exception

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

Traceback

/opt/web/env/lib/python3.5/site-packages/django/urls/resolvers.py in url_patterns
            iter(patterns)
Local Vars

Variable    Value
msg       ("The included URLconf '{name}' 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.')
patterns    <module 'app_name.urls' from '/opt/web/app/my_site/app_name/urls.py'>
self    <URLResolver 'app_name.urls' (None:None) '^/'>

During handling of the above exception ('module' object is not iterable), another exception occurred:

/opt/web/env/lib/python3.5/site-packages/django/core/handlers/exception.py in inner
            response = get_response(request)
[SNIP]

/opt/web/env/lib/python3.5/site-packages/django/urls/base.py in resolve
    return get_resolver(urlconf).resolve(path)
Local Vars

Variable    Value
path      '/4567/details'
urlconf   'app_name.urls'

/opt/web/env/lib/python3.5/site-packages/django/urls/resolvers.py in resolve
            for pattern in self.url_patterns:
Local Vars

Variable    Value
args         ()
kwargs       {}
match        ('4567/details', (), {})
new_path     '4567/details'
path         '/4567/details'
self         <URLResolver 'app_name.urls' (None:None) '^/'>
tried        []

/opt/web/env/lib/python3.5/site-packages/django/utils/functional.py in __get__
        res = instance.__dict__[self.name] = self.func(instance)
Local Vars

Variable    Value
cls         <class 'django.urls.resolvers.URLResolver'>
instance    <URLResolver 'app_name.urls' (None:None) '^/'>
self        <django.utils.functional.cached_property object at 0x7f06c7d2c550>

/opt/web/env/lib/python3.5/site-packages/django/urls/resolvers.py in url_patterns
            raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
Local Vars

Variable    Value
msg        ("The included URLconf '{name}' 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.')
patterns    <module 'app_name.urls' from '/opt/web/app/my_site/app_name/urls.py'>
self    <URLResolver 'app_name.urls' (None:None) '^/'>

Upvotes: 1

Views: 886

Answers (1)

Novfensec
Novfensec

Reputation: 116

Instead of this;

from app_name import views
from app_name.result_view import 
SubmissionDetailView



handler404 ='app_name.views.not_found_view'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('<int:pk>/details',SubmissionDetailView.as_view(), name='details'),
    path('check/', views.check, name='check'),
    path('logout/', views.logout_view, name='logout'),
    path('search/', views.search, name='search'),
    path('features/', views.features, name='features'),
    path('terms-of-service/', views.terms, name='terms'),
    path('privacy-policy/', views.privacy, name='privacy'),
    path('', views.index, name='index'),
]

You should include your app urls to the project urls.py file like this:

Project urls.py:

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/',admin.site.urls),
    path('',include('app_name.urls'))
]

create a urls.py file in app_name folder and then describe your urls like this:

app_name urls.py:

from django.urls import path
from . import views
from app_name.result_view import SubmissionDetailView

handler404 ='views.not_found_view'

urlpatterns = [
    path('<int:pk>/details',SubmissionDetailView.as_view(), name='details'),
    path('check/', views.check, name='check'),
    path('logout/', views.logout_view, name='logout'),
    path('search/', views.search, name='search'),
    path('features/', views.features, name='features'),
    path('terms-of-service/', views.terms, name='terms'),
    path('privacy-policy/', views.privacy, name='privacy'),
    path('', views.index, name='index'),
]

Upvotes: 1

Related Questions