简体   繁体   中英

How to solve No Reverse Match error in django

I am getting this error and I could not solve it, "Reverse for 'post_task' with arguments '('',)' not found. 1 pattern(s) tried: ['post/ajax/task/(?P[0-9]+)$'] " I have a script on my main page , and when I am on the template page everything is ok. but when I go to any other page including the main page, I get this error.

can you assist me? 在此处输入图片说明

main.html

 <script>



  if ($('#task-form').length > 0) {
      // Exists.
      $("#task-form").submit(function (e) {
          // preventing from page reload and default actions

          e.preventDefault();
          // serialize the data for sending the form data.
          var serializedData = $(this).serialize();

          // make POST ajax call
          $.ajax({
              type: 'POST',
              url: "{% url 'post_task' order.id %}",
              data: serializedData,
              success: function (response) {

                  // on successfull creating object
                  // 1. clear the form.
                  $("#task-form").trigger('reset');
                  // 2. focus to nickname input
                  $("#task").focus();

                  // display the newly friend to table.
                  var instance = JSON.parse(response["instance"]);
                  var fields = instance[0]["fields"];
                  $("#my_tasks tbody").prepend(
                      `<tr>
                <td >${fields["task"] || ""}</td>
                </tr>`
                  )
              },
              error: function (response) {

                  // alert the error if any error occured
                  alert(response["responseJSON"]["error"]);
              }
          })
      })
  }

 $("#service-form").submit(function (e) {
    // preventing from page reload and default actions
    e.preventDefault();
    // serialize the data for sending the form data.
    var serializedData = $(this).serialize();
    // make POST ajax call
    $.ajax({
        type: 'POST',
        url: "{% url 'post_service' order.id %}",
        data: serializedData,
        success: function (response) {
            // on successfull creating object
            // 1. clear the form.
            $("#service-form").trigger('reset');
            // 2. focus to nickname input
            $("#id_service").focus();
            var total= $(".total").html(); //saving the total
            $(".total").html(" ");

            // display the newly friend to table.
            var instance = JSON.parse(response["instance"]);
            var fields = instance[0]["fields"];
            $("#my_services tbody").append(
                `<tr>
                <td contenteditable="true">${fields["service"]||""}</td>
                <td contenteditable="true">${fields["quantity"]||""}</td>
                <td contenteditable="true">${fields["price"]||""} ₪</td>
                <td>${total}</td>
                 <td><input type="submit" class="btn btn-danger justify-content-center text-center" value="מחק"  /></td>
                </tr>`
            )
        },
        error: function (response) {
            // alert the error if any error occured
            alert(response["responseJSON"]["error"]);
        }
    })
})

Urls.py

from django.urls import path
from . import views

urlpatterns = [
path('register/', views.registerPage, name="register"),
path('login/', views.loginPage, name="login"),
path('logout/', views.logoutUser, name="logout"),

path('', views.home, name="home"),
path('products/', views.products, name="product"),
path('create_products/', views.product_create_view, name="productcreate"),

###customer###
path('customer/', views.indexView),
path('post/ajax/customer', views.postCustomer, name="post_customer"),
path('get/ajax/validate/name', views.checkCustomerName, name="validate_name"),
path('customer/<str:pk>/', views.customer, name="customer"),

###Order##
path('create_order/<str:pk>', views.createOrder, name="create_order"),
path('update_order/<str:pk>/', views.UpdateOrder, name="update_order"),
path('delete_order/<str:pk>/', views.deleteOrder, name="delete_order"),
path('post/ajax/order/', views.postOrder, name="post_order"),

##Tasks##
path('post/ajax/task/<int:id>', views.postTask, name="post_task"),

##services##
path('post/ajax/service/<int:id>', views.postService, name="post_service"),
path(r'delete_service/', views.deleteService, name="delete_service"),

#file
path(r'^upload/(?P<id>\d+)/$', views.upload_files, name='upload_picture'),

#info
path(r'^info/(?P<id>\d+)/$', views.order_info, name='order_info'),

]

Set the url on the form in your template.

<form id="task-form" action="{% url 'post_task' order.id %}">

Then in your script file, pull the url from the action attribute on the form element:

 $("#service-form").submit(function (e) {
    // preventing from page reload and default actions
    e.preventDefault();
    // serialize the data for sending the form data.
    var serializedData = $(this).serialize();
    // make POST ajax call
    $.ajax({
        type: 'POST',
        url: $(this).attr('action'),

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