【问题标题】:How to solve No Reverse Match error in django如何解决 django 中的无反向匹配错误
【发布时间】:2020-07-13 14:40:03
【问题描述】:

我收到此错误,但无法解决, “未找到带有参数 '('',)' 的 'post_task' 的反向操作。尝试了 1 种模式:['post/ajax/task/(?P[0-9]+)$'] " 我的主页上有一个脚本,当我在模板页面上时,一切正常。 但是当我转到包括主页在内的任何其他页面时,我会收到此错误。

你能帮帮我吗?

ma​​in.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'),

]

【问题讨论】:

  • order{ url 'post_task order.id %} 中不存在。
  • 是的,因为订单在另一个模板中,并且在我的主目录中,我编写了所有脚本。我该如何解决? @WillemVanOnsem

标签: python django ajax django-templates


【解决方案1】:

在模板中的表单上设置 url。

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

然后在你的脚本文件中,从表单元素的 action 属性中提取 url:

 $("#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'),

【讨论】:

  • 谢谢它的工作,你能解释为什么我的代码没有工作吗?出了什么问题?
  • 您的静态文件按原样提供。他们没有 Django 模板上下文或访问 Django 模板中可访问的过滤器和标签。
  • 很高兴我能帮上忙。
猜你喜欢
  • 2021-04-01
  • 2021-11-23
  • 2023-01-25
  • 1970-01-01
  • 2020-11-06
  • 2021-10-02
  • 2021-11-29
  • 2021-06-21
  • 2021-11-30
相关资源
最近更新 更多