Django - NoReverseMatch at /.... Reverse for ... with arguments ("",52)
Django - NoReverseMatch at /.... Reverse for ... with arguments ("",52)
查看了很多类似的NoReverseMatch Errors 都无法解决我的问题。这是在 Django 中。
我不断收到:
/equipment32 上的 NoReverseMatch
对 'equipment' 进行反转,未找到参数 '('', 32)'。尝试了 1 种模式:['equipment(?P<equipment_id>[0-9]+)$']
这是回溯:
Traceback (most recent call last):
File "C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Albert\Desktop\HARVARD CS50\tracker_django\tracker\views.py", line 128, in equipment
return render(request, "jobs/equipment.html", context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\defaulttags.py", line 443, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\urls\base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\ProgramData\Miniconda3\lib\site-packages\django\urls\resolvers.py", line 677, in _reverse_with_prefix
raise NoReverseMatch(msg)
这是我的模板 job.html,它生成 links:
<table style="width: 900px">
<caption style="font-size: 25px">Equipment in Scope</caption>
<a href="#equipment_form" style="font-size: 20px">Add Equipment</a>
<tr>
<th>Site ID</th>
<th>Equipment Type</th>
<th>Manufacturer</th>
<th>Model</th>
<th>Test Sheet</th>
</tr>
{% for equipment in equipments %}
<tr>
{% csrf_token %}
<td><a href="{% url 'equipment' equipment.id %}">{{equipment.site_id}}</a></td>
<td>{{equipment.template.classification}}</td>
<td>{{equipment.manufacturer}}</td>
<td>{{equipment.model}}</td>
<td><a href="www.drive.google.com">Test Sheet</td>
</tr>
{% empty %}
<li>No Equipment</li>
{% endfor %}
</table> <br>
这似乎可以很好地加载并为每个设备对象创建 links。这是我的 urls.py:
from django.urls import path
from . import views, constructors
from .models import Job, FSR, Template, Equipment, TestEquipment
urlpatterns = [
path("equipment<int:equipment_id>", views.equipment, name="equipment"),
]
这个url应该link到views.equipment;这里是:
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.urls import reverse
from .models import Job, FSR, Template, Equipment, TestEquipment
def equipment(request, equipment_id):
if not request.user.is_authenticated:
return render(request, "jobs/login.html", {"message": None})
try:
equipment=Equipment.objects.get(pk=equipment_id)
except Equipment.DoesNotExist:
raise Http404("Equipment does not exist.")
context = {
"equipment": equipment,
"templates": Template.objects.all(),
"test_equipments": equipment.template.recommended_test_equipment.all(),
}
if request.user.is_authenticated:
return render(request, "jobs/equipment.html", context)
应该生成我实际的 equipment.html:
{% extends "jobs/base.html"%}
{% block title %}
{{equipment.site_id}}
{% endblock %}
{% block body %}
<h1 style = "text-align: center">{{equipment.site_id}} ({{equipment.template.classification}})</h1>
<style>table, th, td {
border: 1px solid black;
border-collapse: collapse;
}</style>
<table style="width:900px">
<caption style="font-size: 25px">Equipment Properties</caption>
<tr>
<td>Equipment Type</td>
<td><b>{{equipment.template.classification}}</b></td>
</tr>
<tr>
<td>Job Site ID or Name</td>
<td><b>{{equipment.site_id}}</b></td>
</tr>
<tr>
<td>Manufacturer</td>
<td><b>{{equipment.manufacturer}}</b></td>
</tr>
<tr>
<td>Model</td>
<td><b>{{equipment.model}}</b></td>
</tr>
<tr>
<td>Link to Manual</td>
<td><a href="https://www.eaton.com"><b>{{equipment.manual}}</b></a></td>
</tr>
<tr>
<td>Link to Item Folder</td>
<td><a><b>{{equipment.template.cloud_link}}</b></a></td>
</tr>
<tr>
<td>Equipment Questions</td>
<td><a><b>{{equipment.template.equipment_questions}}</b></a></td>
</tr>
<tr>
<td>Answers</td>
<!-- <form action="{% url 'equipment' job.id equipment.id%}" method="post">
<td><a><b><textarea id="scope" name="scope" rows="4" cols="55" >{{ equipment.equipment_answers }}</textarea><input type="submit" value="Confirm Answers">
<input type="reset"></b></a></td>
</form> -->
</tr>
<tr>
<td>Scope for This Equipment</td>
<td><a><b>{{equipment.scope}}</b></a></td>
</tr>
<tr>
<td>Additional Notes</td>
<td><a><b>{{equipment.notes}}</b></a></td>
</tr>
</table><br>
<h3>Recommended Test Equpiment</h3>
<ul>
{% for test_equipment in test_equipments %}
<li>
{% csrf_token %}
{{ test_equipment }}
</li>
{% empty %}
<li>No Equipment</li>
{% endfor %}
</ul>
<button onclick="goBack()">Back to Job Page</button>
<script>
function goBack() {
window.history.back();
}
</script>
{% endblock %}
我不明白为什么它似乎要求两个参数 equipment("", 35) 因为视图函数只接受 equipment_id。非常感谢任何帮助!
在您的模板 equipment.html
中,您提供了两个参数,只是删除 job.id
。虽然有评论但应该是正确的urlpattern
<form action="{% url 'equipment' equipment.id%}" method="post">
<td><a><b><textarea id="scope" name="scope" rows="4" cols="55" >{{ equipment.equipment_answers }}</textarea><input type="submit" value="Confirm Answers">
<input type="reset"></b></a></td>
</form>
我的问题出在 equipment.html 代码的注释部分。我有一个注释掉的部分,其中包括:
<form action="{% url 'equipment' equipment.id job.id method="post" %}>....
事实证明,django url 解析器总是处理 url 标记,无论是否注释,上面的 html 代码试图发送 url 2 个参数,而我的 url 路径只需要 1 个参数。
查看了很多类似的NoReverseMatch Errors 都无法解决我的问题。这是在 Django 中。
我不断收到: /equipment32 上的 NoReverseMatch 对 'equipment' 进行反转,未找到参数 '('', 32)'。尝试了 1 种模式:['equipment(?P<equipment_id>[0-9]+)$']
这是回溯:
Traceback (most recent call last):
File "C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Albert\Desktop\HARVARD CS50\tracker_django\tracker\views.py", line 128, in equipment
return render(request, "jobs/equipment.html", context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\template\defaulttags.py", line 443, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\ProgramData\Miniconda3\lib\site-packages\django\urls\base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\ProgramData\Miniconda3\lib\site-packages\django\urls\resolvers.py", line 677, in _reverse_with_prefix
raise NoReverseMatch(msg)
这是我的模板 job.html,它生成 links:
<table style="width: 900px">
<caption style="font-size: 25px">Equipment in Scope</caption>
<a href="#equipment_form" style="font-size: 20px">Add Equipment</a>
<tr>
<th>Site ID</th>
<th>Equipment Type</th>
<th>Manufacturer</th>
<th>Model</th>
<th>Test Sheet</th>
</tr>
{% for equipment in equipments %}
<tr>
{% csrf_token %}
<td><a href="{% url 'equipment' equipment.id %}">{{equipment.site_id}}</a></td>
<td>{{equipment.template.classification}}</td>
<td>{{equipment.manufacturer}}</td>
<td>{{equipment.model}}</td>
<td><a href="www.drive.google.com">Test Sheet</td>
</tr>
{% empty %}
<li>No Equipment</li>
{% endfor %}
</table> <br>
这似乎可以很好地加载并为每个设备对象创建 links。这是我的 urls.py:
from django.urls import path
from . import views, constructors
from .models import Job, FSR, Template, Equipment, TestEquipment
urlpatterns = [
path("equipment<int:equipment_id>", views.equipment, name="equipment"),
]
这个url应该link到views.equipment;这里是:
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.urls import reverse
from .models import Job, FSR, Template, Equipment, TestEquipment
def equipment(request, equipment_id):
if not request.user.is_authenticated:
return render(request, "jobs/login.html", {"message": None})
try:
equipment=Equipment.objects.get(pk=equipment_id)
except Equipment.DoesNotExist:
raise Http404("Equipment does not exist.")
context = {
"equipment": equipment,
"templates": Template.objects.all(),
"test_equipments": equipment.template.recommended_test_equipment.all(),
}
if request.user.is_authenticated:
return render(request, "jobs/equipment.html", context)
应该生成我实际的 equipment.html:
{% extends "jobs/base.html"%}
{% block title %}
{{equipment.site_id}}
{% endblock %}
{% block body %}
<h1 style = "text-align: center">{{equipment.site_id}} ({{equipment.template.classification}})</h1>
<style>table, th, td {
border: 1px solid black;
border-collapse: collapse;
}</style>
<table style="width:900px">
<caption style="font-size: 25px">Equipment Properties</caption>
<tr>
<td>Equipment Type</td>
<td><b>{{equipment.template.classification}}</b></td>
</tr>
<tr>
<td>Job Site ID or Name</td>
<td><b>{{equipment.site_id}}</b></td>
</tr>
<tr>
<td>Manufacturer</td>
<td><b>{{equipment.manufacturer}}</b></td>
</tr>
<tr>
<td>Model</td>
<td><b>{{equipment.model}}</b></td>
</tr>
<tr>
<td>Link to Manual</td>
<td><a href="https://www.eaton.com"><b>{{equipment.manual}}</b></a></td>
</tr>
<tr>
<td>Link to Item Folder</td>
<td><a><b>{{equipment.template.cloud_link}}</b></a></td>
</tr>
<tr>
<td>Equipment Questions</td>
<td><a><b>{{equipment.template.equipment_questions}}</b></a></td>
</tr>
<tr>
<td>Answers</td>
<!-- <form action="{% url 'equipment' job.id equipment.id%}" method="post">
<td><a><b><textarea id="scope" name="scope" rows="4" cols="55" >{{ equipment.equipment_answers }}</textarea><input type="submit" value="Confirm Answers">
<input type="reset"></b></a></td>
</form> -->
</tr>
<tr>
<td>Scope for This Equipment</td>
<td><a><b>{{equipment.scope}}</b></a></td>
</tr>
<tr>
<td>Additional Notes</td>
<td><a><b>{{equipment.notes}}</b></a></td>
</tr>
</table><br>
<h3>Recommended Test Equpiment</h3>
<ul>
{% for test_equipment in test_equipments %}
<li>
{% csrf_token %}
{{ test_equipment }}
</li>
{% empty %}
<li>No Equipment</li>
{% endfor %}
</ul>
<button onclick="goBack()">Back to Job Page</button>
<script>
function goBack() {
window.history.back();
}
</script>
{% endblock %}
我不明白为什么它似乎要求两个参数 equipment("", 35) 因为视图函数只接受 equipment_id。非常感谢任何帮助!
在您的模板 equipment.html
中,您提供了两个参数,只是删除 job.id
。虽然有评论但应该是正确的urlpattern
<form action="{% url 'equipment' equipment.id%}" method="post">
<td><a><b><textarea id="scope" name="scope" rows="4" cols="55" >{{ equipment.equipment_answers }}</textarea><input type="submit" value="Confirm Answers">
<input type="reset"></b></a></td>
</form>
我的问题出在 equipment.html 代码的注释部分。我有一个注释掉的部分,其中包括:
<form action="{% url 'equipment' equipment.id job.id method="post" %}>....
事实证明,django url 解析器总是处理 url 标记,无论是否注释,上面的 html 代码试图发送 url 2 个参数,而我的 url 路径只需要 1 个参数。