简体   繁体   中英

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/

Where object_id is the id of the object of model Client .

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 . When I click on this button it takes me to the following 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 . I want that If I click on Cancel it should get back to the following URL:

<local_server>/object_id/

And for that I have the following template for adding installment, where you can see the Cancel button.

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 . I have written the object.id in that line, but when I click on Add Installment I am getting the following error:

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:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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