【发布时间】:2020-07-13 14:40:03
【问题描述】:
我收到此错误,但无法解决, “未找到带有参数 '('',)' 的 'post_task' 的反向操作。尝试了 1 种模式:['post/ajax/task/(?P[0-9]+)$'] " 我的主页上有一个脚本,当我在模板页面上时,一切正常。 但是当我转到包括主页在内的任何其他页面时,我会收到此错误。
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'),
]
【问题讨论】:
-
order在{ url 'post_task order.id %}中不存在。 -
是的,因为订单在另一个模板中,并且在我的主目录中,我编写了所有脚本。我该如何解决? @WillemVanOnsem
标签: python django ajax django-templates