[英]Django: No ReverseMatch. How to reverse to the previous URL?
I have a model class Client
and the URL for this model object is I have a model class Client
and the URL for this model object is
<local_server>/object_id/
Where object_id
is the id
of the object of model Client
.其中object_id
是 model Client
的 object 的id
。
Now, when I am at this URL, there is a button on the page called Add Installment
to add the installment for that particular Client
.现在,当我在此 URL 时,页面上有一个名为Add Installment
Installment 的按钮,用于为该特定Client
添加分期付款。 When I click on this button it takes me to the following URL:当我单击此按钮时,它会将我带到以下 URL:
<local_server>/object_id/add_installment
Now if I add the new installment, it works fine.现在,如果我添加新的分期付款,它工作正常。 But the Add Installment
page has two buttons, Add
and Cancel
.但是Add Installment
页面有两个按钮, Add
和Cancel
。 I want that If I click on Cancel
it should get back to the following URL:我希望如果我单击Cancel
,它应该返回到以下 URL:
<local_server>/object_id/
And for that I have the following template for adding installment, where you can see the Cancel
button.为此,我有以下用于添加分期付款的模板,您可以在其中看到“ Cancel
”按钮。
installment_form.html installment_form.html
{% extends "client_management_system/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content_section">
<form method="post">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4"> Add New Installment</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-success" type="submit">Add Installment</button>
<a class="btn btn-outline-danger" type = "submit" href="{% url 'client_details' object.id %}" >Cancel</a>
</div>
</form>
</div>
{% endblock %}
In the Cancel
button, client_details
is the name of the URL <local_server>/object_id
.在Cancel
按钮中, client_details
是 URL <local_server>/object_id
的名称。 I have written the object.id
in that line, but when I click on Add Installment
I am getting the following error:我在该行中写了object.id
,但是当我单击Add Installment
时,我收到以下错误:
NoReverseMatch at /1/Add-Installment
Reverse for 'client_details' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<pk>[0-9]+)\\/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/1/Add-Installment
Django Version: 3.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'client_details' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<pk>[0-9]+)\\/$']
Exception Location: /anaconda3/lib/python3.6/site-packages/django/urls/resolvers.py, line 694, in _reverse_with_prefix
Python Executable: /anaconda3/bin/python3
Python Version: 3.6.10
Python Path:
['/Users/razajaved/Documents/installment_plan',
'/anaconda3/lib/python36.zip',
'/anaconda3/lib/python3.6',
'/anaconda3/lib/python3.6/lib-dynload',
'/anaconda3/lib/python3.6/site-packages',
'/anaconda3/lib/python3.6/site-packages/aeosa']
Server time: Wed, 21 Apr 2021 20:49:32 +0000
Can someone please help me in this?有人可以帮我吗?
I found two ways to do this:我找到了两种方法来做到这一点:
Go back to the previous URL <local_server>/object_id/
by replacing the following line in my html file: Go 回到以前的 URL <local_server>/object_id/
通过替换我的 html 文件中的以下行:
<a class="btn btn-outline-danger" type = "submit" href="{% url 'client_details' object.id %}" >Cancel</a>
with this:有了这个:
<input type=button class="btn btn-outline-danger" value="Cancel" onClick="window.history.back();return false;">
OR with this:或者这样:
<a class="btn btn-outline-danger" type = "submit" href="javascript:history.go(-1)">Cancel</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.