question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditionally adding silk urls leads to NoReverseMatch exception when running tests

See original GitHub issue

I have the following in my root urls.py:

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        path('__debug__/', include(debug_toolbar.urls)),
        path('silk/', include('silk.urls', namespace='silk'))
    ] + urlpatterns

This works fine under runserver, however when running tests I get:

Traceback (most recent call last):
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/urls/base.py", line 75, in reverse
    extra, resolver = resolver.namespace_dict[ns]
KeyError: 'silk'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/silk/middleware.py", line 23, in silky_reverse
    r = reverse('silk:%s' % name, *args, **kwargs)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/urls/base.py", line 86, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'silk' is not a registered namespace

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/liav/dev/grizzly.backend/manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/commands/test.py", line 26, in run_from_argv
    super().run_from_argv(argv)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/commands/test.py", line 56, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/test/runner.py", line 605, in run_tests
    self.run_checks()
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/test/runner.py", line 567, in run_checks
    call_command('check', verbosity=self.verbosity)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/__init__.py", line 148, in call_command
    return command.execute(*args, **defaults)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/commands/check.py", line 65, in handle
    fail_level=getattr(checks, options['fail_level']),
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/management/base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/debug_toolbar/apps.py", line 34, in check_middleware
    if is_middleware_class(GZipMiddleware, middleware):
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/debug_toolbar/apps.py", line 75, in is_middleware_class
    middleware_cls = import_string(middleware_path)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 726, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/silk/middleware.py", line 32, in <module>
    fpath = silky_reverse('summary')
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/silk/middleware.py", line 28, in silky_reverse
    r = reverse(name, *args, **kwargs)
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/urls/base.py", line 90, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/home/liav/.virtualenvs/grizzly.backend-POxoeTdG/lib/python3.7/site-packages/django/urls/resolvers.py", line 622, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'summary' not found. 'summary' is not a valid view function or pattern name.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
m-ajaycommented, Nov 13, 2018
if DEBUG:
    INSTALLED_APPS.append('silk')

In settings.py

and

if 'silk' in settings.INSTALLED_APPS:
    urlpatterns = [
        path('silk/', include('silk.urls', namespace='silk')),
    ] + urlpatterns

in urls.py solved this issue for me.

0reactions
SebCorbincommented, Oct 27, 2022

The original issue was about silk’s urls not being properly added, please ensure they are and open another issue with complete info about your code if needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Test - django.urls.exceptions.NoReverseMatch
I got errors when running tests django.urls.exceptions.NoReverseMatch: Reverse for 'interfacegraph' with no arguments not found.
Read more >
django-silk Changelog - pyup.io
427 Passed wsgi request to SILKY_PYTHON_PROFILER_FUNC - Added Django 3.1 & 3.2 to test matrix - Replace url with re_path for Django 4.0...
Read more >
Conditional Testing - cypress - w3resource
I want to find all <li> elements in a page automatically and then based on the ones that are found, I want to...
Read more >
Django saying 'course' isn't a registered namespace upon ...
ERROR: test_course_list_view (courses.tests. ... raise NoReverseMatch("%s is not a registered namespace" % key) django.urls.exceptions.
Read more >
Where do I find a list of the errors that SilkPerformer reports ...
All the errors that SilkPerformer reports when running tests against web applications can be found as follows:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found