繁体   English   中英

Django:无反向匹配。 如何反转到之前的URL?

[英]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页面有两个按钮, AddCancel 我希望如果我单击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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM