我正在尝试通过 URL 将查询参数传递到 Django 视图中,如下所示:
127.0.0.1:8000/vysledky/?a_id=1
但是,每当我尝试访问该 URL 时,都会出现NoReverseMatch错误。完整的错误报告如下所示:
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.
这是我的观点的代码:
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)
这是我在项目的另一个页面中用来链接到该页面的超链接:
<a href="{% url 'results' %}{{ a.id }}/">{{ a.title }} - {{ a.date }}</a>
由于某种原因,当我第一次测试它时,这个设置运行良好,但在随后的测试中,它开始每次都出现这个错误。自第一次测试以来,我所做的唯一更改是添加另一个视图并在开始给出错误的视图模板中添加指向它的链接。