#29068 closed Bug (needsinfo)
Django urlpatterns has limit of allowed urls with namespace?
Reported by: | Gerben Morsink | Owned by: | nobody |
---|---|---|---|
Component: | Core (URLs) | Version: | 1.11 |
Severity: | Normal | Keywords: | urlpatterns limit |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'm currently working on a huge site with a huge number of distinct urlpatterns, that are categorized in a tree pattern, starting with the project's urls.py and then going down the tree with include statements (sometimes namespaced), which in turn have their own include statements, untill we are about 4 branches away.
Next to that many urlpatterns are conditional (if settings.DEBUG ... if 'app_x' in installed_apps... etc).
I have had issues with one of the last urlpatterns in the project's urls.py a lot on my production environment, on apache + wsgi ('No reverse found'), which I couldn't reproduce on my development machine with runserver . It has a namespace set. It was not a user facing endpoint so I forgot about it.
Now I have added another urlpattern to the project's urls.py with a namespace and it too gives 'No reverse found' (it is reversed in a middleware, so the app does not start at all, making it easier to test).
When I place the last urlpattern up in the projects urls.py that it comes as the second urlpattern, the app does start, so it looks like the urlpatterns are going into some kind of limit.
For your reference this is the code that works when placed after the first urlpatterns definition, but not when placed at the bottom (I think nothing special).
if 'silk' in settings.INSTALLED_APPS: urlpatterns += [url(r'^silk/', include('silk.urls', namespace='silk'))]
Change History (2)
comment:1 by , 7 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:2 by , 7 years ago
I have found the issue.
I had an url of type:
url(r'^aboutus/$', RedirectView.as_view(url=reverse('aboutus'), permanent=True))
in my urls.py. Where the 'aboutus' url was defined somewhere above in an included sub urls.py.
With the Django testserver this works fine, but in production (Apache + WSGI) it gives a lot of seemingly strange errors, such as unexpected 404s and (after including the package silk) unexpected 500 errors. The strange thing was, that it did work on the test server (also Apache + WSGI), but not on the live server. It gave me some pulled out grey hair, so I hope someone finds this anwers when the need is there.
For your info, I only find out about this issue (and the fact that it lead to a lot of 404s) after I installed Silk and saw the following stacktrace:
[Fri Feb 02 20:42:49.035335 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] Traceback (most recent call last): [Fri Feb 02 20:42:49.035375 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/django/urls/base.py", line 77, in reverse [Fri Feb 02 20:42:49.035382 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] extra, resolver = resolver.namespace_dict[ns] [Fri Feb 02 20:42:49.035392 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/django/urls/resolvers.py", line 342, in namespace_dict [Fri Feb 02 20:42:49.035398 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] return self._namespace_dict[language_code] [Fri Feb 02 20:42:49.035420 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] KeyError: 'en-gb' [Fri Feb 02 20:42:49.035433 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] [Fri Feb 02 20:42:49.035437 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] During handling of the above exception, another exception occurred: [Fri Feb 02 20:42:49.035442 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] [Fri Feb 02 20:42:49.035450 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] Traceback (most recent call last): [Fri Feb 02 20:42:49.035474 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/silk/middleware.py", line 33, in silky_reverse [Fri Feb 02 20:42:49.035479 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] r = reverse('silk:%s' % name, *args, **kwargs) [Fri Feb 02 20:42:49.035489 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/django/urls/base.py", line 87, in reverse [Fri Feb 02 20:42:49.035494 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] raise NoReverseMatch("%s is not a registered namespace" % key) [Fri Feb 02 20:42:49.035512 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] django.urls.exceptions.NoReverseMatch: 'en-gb' is not a registered namespace [Fri Feb 02 20:42:49.035523 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] [Fri Feb 02 20:42:49.035527 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] During handling of the above exception, another exception occurred: [Fri Feb 02 20:42:49.035531 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] [Fri Feb 02 20:42:49.035540 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] Traceback (most recent call last): [Fri Feb 02 20:42:49.035643 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/start-overnew-with-git-version-control/backend/framework/framework/wsgi.py", line 14, in <module> [Fri Feb 02 20:42:49.035655 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] application = get_wsgi_application() [Fri Feb 02 20:42:49.035673 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application [Fri Feb 02 20:42:49.035679 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] return WSGIHandler() [Fri Feb 02 20:42:49.035688 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 151, in __init__ [Fri Feb 02 20:42:49.035694 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] self.load_middleware() [Fri Feb 02 20:42:49.035702 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/django/core/handlers/base.py", line 56, in load_middleware [Fri Feb 02 20:42:49.035708 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] mw_class = import_string(middleware_path) [Fri Feb 02 20:42:49.035727 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/django/utils/module_loading.py", line 20, in import_string [Fri Feb 02 20:42:49.035732 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] module = import_module(module_path) [Fri Feb 02 20:42:49.035741 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/usr/local/lib/python3.5/importlib/__init__.py", line 126, in import_module [Fri Feb 02 20:42:49.035746 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] return _bootstrap._gcd_import(name[level:], package, level) [Fri Feb 02 20:42:49.035755 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "<frozen importlib._bootstrap>", line 985, in _gcd_import [Fri Feb 02 20:42:49.035764 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "<frozen importlib._bootstrap>", line 968, in _find_and_load [Fri Feb 02 20:42:49.035774 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked [Fri Feb 02 20:42:49.035783 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "<frozen importlib._bootstrap>", line 673, in _load_unlocked [Fri Feb 02 20:42:49.035792 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "<frozen importlib._bootstrap_external>", line 697, in exec_module [Fri Feb 02 20:42:49.035801 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed [Fri Feb 02 20:42:49.035811 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/silk/middleware.py", line 42, in <module> [Fri Feb 02 20:42:49.035816 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] fpath = silky_reverse('summary') [Fri Feb 02 20:42:49.035825 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/silk/middleware.py", line 38, in silky_reverse [Fri Feb 02 20:42:49.035830 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] r = reverse(name, *args, **kwargs) [Fri Feb 02 20:42:49.035839 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/django/urls/base.py", line 91, in reverse [Fri Feb 02 20:42:49.035844 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))) [Fri Feb 02 20:42:49.035858 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/django/urls/resolvers.py", line 438, in _reverse_with_prefix [Fri Feb 02 20:42:49.035863 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] possibilities = self.reverse_dict.getlist(lookup_view) [Fri Feb 02 20:42:49.035872 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] File "/home/ webapps/production_v5/lib/python3.5/site-packages/django/urls/resolvers.py", line 335, in reverse_dict [Fri Feb 02 20:42:49.035877 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] return self._reverse_dict[language_code] [Fri Feb 02 20:42:49.035896 2018] [wsgi:error] [pid 169677:tid 139961307232000] [remote 127.0.0.1:48276] KeyError: 'en-gb'
You should debug your code and confirm that Django is at fault before creating a ticket. We can't triage the issue without a sample project to reproduce it. Please reopen the ticket if you can do one of those things. Thanks.