0

I am trying to pass a query argument into a Django view through the URL, like so:
127.0.0.1:8000/vysledky/?a_id=1

However, whenever I try to go to that URL, it gives me a NoReverseMatch error. The full error report is listed below:

Request Method: GET
Request URL: http://127.0.0.1:8000/vysledky/?a_id=1

Django Version: 3.0.5
Python Version: 3.8.0
Installed Applications:
['analysis.apps.AnalysisConfig',
 'users.apps.UsersConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /Users/vojtamazur/Documents/CS_IA/mysite/analysis/templates/analysis/base.html, error at line 0
   Reverse for '' not found. '' is not a valid view function or pattern name.
   1 : <!DOCTYPE html>
   2 : <html>
   3 : <head>
   4 :     {% if title %}
   5 :         <title> Analýza - {{ title }} </title>
   6 :     {% else %}
   7 :         <title> Analýza </title>
   8 :     {% endif %}
   9 : </head>
   10 : <body>


Traceback (most recent call last):
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/vojtamazur/Documents/CS_IA/mysite/analysis/views.py", line 167, in dispatch
    return super().dispatch(*args, **kwargs)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/vojtamazur/Documents/CS_IA/mysite/analysis/views.py", line 178, in get
    return render(request, self.template_name, context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/template/defaulttags.py", line 443, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/urls/base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/Users/vojtamazur/Documents/CS_IA/env/lib/python3.8/site-packages/django/urls/resolvers.py", line 677, in _reverse_with_prefix
    raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /vysledky/
Exception Value: Reverse for '' not found. '' is not a valid view function or pattern name.

Here is the code for my view:

class ResultsView(View):
    template_name = "analysis/results.html"

    def get(self, request):
        analysis_id = request.GET.get('a_id')
        analysis = Analysis.objects.get(pk=analysis_id)
        clients = analysis.client_set.all()
        context = {
            'analysis':analysis,
            'clients':clients,
        }
        return render(request, self.template_name, context)

This is the hyperlink that I used in another page of the project to link to this page:
<a href="{% url 'results' %}{{ a.id }}/">{{ a.title }} - {{ a.date }}</a> For some reason, this set up worked fine when I first tested it, but on subsequent testing, it started giving this error every time. The only change that I did since the first test was to add another view and add a link to it in the template of the view that started giving errors.

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
Vojta Ma
  • 21
  • 1
  • 2
  • Please show your urls.py - url reversion is based on these settings. – Ivan Starostin Apr 11 '20 at 10:10
  • You better use: `{% url 'results' a.id %}` and for naming convention I would use `id` instead `a_id` (because it's clear that this is an analysis' id) and instead `a` (which tells us nothing about the variable) you better use `analysis`. – Myzel394 Apr 11 '20 at 10:18

2 Answers2

0

Reverse for '' not found tells you that you give the empty string '' as URL, and that is not found in your urls.py.

Probably a.id is an empty string. You can "print" {{a.id}} to see the value you have passed into context

Timothé Delion
  • 1,310
  • 12
  • 17
0

I found the solution. The problem was in the template 'results.html'. There was a hyperlink that linked to a misspelled URL path() name, which probably caused the error during the template rendering. When I spelled the name correctly, it worked fine.

Vojta Ma
  • 21
  • 1
  • 2