[英]Django: No ReverseMatch. How to reverse to the previous URL?
I have a model class Client
and the URL for this model object is
<local_server>/object_id/
其中object_id
是 model Client
的 object 的id
。
現在,當我在此 URL 時,頁面上有一個名為Add Installment
Installment 的按鈕,用於為該特定Client
添加分期付款。 當我單擊此按鈕時,它會將我帶到以下 URL:
<local_server>/object_id/add_installment
現在,如果我添加新的分期付款,它工作正常。 但是Add Installment
頁面有兩個按鈕, Add
和Cancel
。 我希望如果我單擊Cancel
,它應該返回到以下 URL:
<local_server>/object_id/
為此,我有以下用於添加分期付款的模板,您可以在其中看到“ Cancel
”按鈕。
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 %}
在Cancel
按鈕中, client_details
是 URL <local_server>/object_id
的名稱。 我在該行中寫了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
有人可以幫我嗎?
我找到了兩種方法來做到這一點:
Go 回到以前的 URL <local_server>/object_id/
通過替換我的 html 文件中的以下行:
<a class="btn btn-outline-danger" type = "submit" href="{% url 'client_details' object.id %}" >Cancel</a>
有了這個:
<input type=button class="btn btn-outline-danger" value="Cancel" onClick="window.history.back();return false;">
或者這樣:
<a class="btn btn-outline-danger" type = "submit" href="javascript:history.go(-1)">Cancel</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.