django - 模板渲染时出错 未找到“组编辑”的反向。'group-edit' 不是有效的视图函数或模式名称
问题描述
加载“group_list.html”时出现错误:找不到“group-edit”的反向。'group-edit' 不是有效的视图函数或模式名称。
如果我禁止此 href 引用,它可以工作,但我需要它才能编辑组实例
这是我对 group_edit 的views.py:
def group_edit(request, group_id):
group_form = GroupFormEdit(instance=Group.objects.get(id=group_id))
if request.method == "POST":
group_form = GroupForm(request.POST, instance=Group.objects.get(id=group_id))
if group_form.is_valid():
group_form.save()
messages.success(request, 'Group saved') # message for inform user of success - See messages in html file
return redirect('home')
else:
group_form = GroupForm()
return render(request, 'imports/group_edit.html', {
"group_form": group_form,
})
我的 group_list.html:
{% block page %}
<div class="panel-body">
<table class="table table-bordered table-hover table-striped col-md-3">
<thead class="thead-dark">
<tr class="text-center">
<th>Group Name</th>
<th>Parent Name</th>
</tr>
</thead>
<tbody>
{% for group in groups %}
<tr>
<td scope="row" class="col-md-3"><a href="{% url 'group-edit' group.group_id %}">{{ group.group_name }}</a></td>
<td class="col-md-3">{{ group.groupParent_id }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
我的 urls.py:
urlpatterns = [
path('', views.imports_last, name='home'),
path('company_create/', views.company_create, name='company_creation'),
path('group_create/', views.group_create, name='group_creation'),
path('group_edit/', views.group_edit, name='group_edit'),
path('group_list/', views.group_list, name='group_list'),
]
和models.py:
class Group(models.Model):
group_id = models.AutoField(primary_key=True)
groupParent_id = models.ForeignKey('self', blank=True, null=True, related_name='Parent', on_delete=models.CASCADE)
group_name = models.CharField(max_length=100, null=False, blank=False, unique=True)
def __str__(self):
return '{}'.format(self.group_name)
解决方案
我找到了我的痛点;我应该在 url 中提到我想添加一个变量:
path('group_edit/<int:group_id>/', views.group_edit, name='group-edit'),
推荐阅读
- kubernetes - 如何在 Kubernetes 中设置 HTTPS 负载均衡器
- c++ - 使用 long_options 解析具有任意数量 required_arguments 的选项
- php - PHP - 多维关联数组中的显式键
- c# - CMS 版本 2.2 SHA256WithRSAEncryption
- regex - 使用正则表达式查找和重命名文件/文件夹
- python - python GUI倒数计时器(不要使用类)
- apache-spark - Spark SQL 2.3+ 是否支持 UDT?
- python - 无法在 mysql 版本 8.0.12 中使用 LEAD
- reactjs - 通过比较 React 中的数组和状态来创建可编程路由
- amazon-dynamodb - 结构产品和类别 - DynamoDB