Существует CBV
(JointProductsDetail
), который отвечает за рендеринг шаблона.В шаблоне я пытаюсь выполнить запрос ajax
(тип get
).Я указываю url
.Запрос не приходит к указанной функции, но я получаю success
в ответе.
template
<form>
<a href='#' class='add_to_cart'>
<button class='btn btn-danger' id="make_an_order" type="button">abc</button>
</a>
</form>
<script>
$(document).ready(function(){
$('.add_to_cart').on('click', function(e){
$.ajax({
type: "GET",
url: "{% url 'add_to_cart' %}",
data: {},
success: function(data){
alert('Товар добавлен в корзину');
location.reload();
},
error: function(data){
alert('Произошла ошибка. Попробуйте позже')
}
})
})
})
</script>
urls
from django.conf.urls import url
from products.views import (
Categories, ProductsOrderStatus, ProductsOrderCancel,
Products, ProductsCreate, ProductsDetail, ProductsUpdate,
ProductsDelete, ProductsCopy, ProductsReport,
JointProducts, JointProductsCreate, JointProductsDetail,
JointProductsUpdate, JointProductsDelete, JointProductsCopy,
JointProductsReport, add_to_cart_view, orders_week_view,
orders_month_view, orders_year_view, bills_week_view,
bills_month_view, bills_year_view, basket_view, cart_true_view,
remove_cart_view, BillDetail, privat24
)
urlpatterns = [
url(r'^(?P<section_slug>products|joint_products)/$',
Categories.as_view(),
name='products_categories_list'),
url(r'^(?P<section_slug>products|joint_products)/orders/(?P<pk>[\d]+)/status/$',
ProductsOrderStatus.as_view(),
name='products_order_status'),
url(r'^(?P<section_slug>products|joint_products)/orders/(?P<pk>[\d]+)/cancel/$',
ProductsOrderCancel.as_view(),
name='products_order_cancel'),
url(r'^products/add/$', ProductsCreate.as_view(), name='products_create'),
url(r'^products/(?P<slug>[-\w]+)/add/$',
ProductsCreate.as_view(),
name='products_create'),
url(r'^products/(?P<slug>[-\w]+)/$',
Products.as_view(),
name='products_list'),
url(r'^products/(?P<pk>[\d]+)/(?P<slug>[-\w]+)/$',
ProductsDetail.as_view(),
name='products_detail'),
url(r'^products/(?P<pk>[\d]+)/(?P<slug>[-\w]+)/update/$',
ProductsUpdate.as_view(),
name='products_update'),
url(r'^products/(?P<pk>[\d]+)/(?P<slug>[-\w]+)/delete/$',
ProductsDelete.as_view(),
name='products_delete'),
url(r'^products/(?P<pk>[\d]+)/(?P<slug>[-\w]+)/copy/$',
ProductsCopy.as_view(),
name='products_copy'),
url(r'^products/(?P<pk>[\d]+)/(?P<slug>[-\w]+)/report/$',
ProductsReport.as_view(),
name='products_report'),
url(r'^joint_products/(?P<slug>[a-zA-Z0-9-][^_]+)/add/$',
JointProductsCreate.as_view(),
name='joint_products_create'),
url(r'^joint_products/(?P<pk>[\d]+)/(?P<slug>[-\w]+)/$',
JointProductsDetail.as_view(),
name='joint_products_detail'),
url(r'^joint_products/(?P<pk>[\d]+)/(?P<slug>[-\w]+)/update/$',
JointProductsUpdate.as_view(),
name='joint_products_update'),
url(r'^joint_products/(?P<pk>[\d]+)/(?P<slug>[-\w]+)/delete/$',
JointProductsDelete.as_view(),
name='joint_products_delete'),
url(r'^joint_products/(?P<pk>[\d]+)/(?P<slug>[-\w]+)/copy/$',
JointProductsCopy.as_view(),
name='joint_products_copy'),
url(r'^joint_products/(?P<pk>[\d]+)/(?P<slug>[-\w]+)/report/$',
JointProductsReport.as_view(),
name='joint_products_report'),
url(r'^joint_products/(?P<slug>[a-zA-Z0-9-][^_]+)/$',
JointProducts.as_view(),
name='joint_products_list'),
url(r'^add_to_cart/$', add_to_cart_view, name='add_to_cart'),
url(r'^remove_cart/$', remove_cart_view, name='remove_cart'),
url(r'^orders_week/$', orders_week_view, name='orders_week'),
url(r'^orders_month/$', orders_month_view, name='orders_month'),
url(r'^orders_year/$', orders_year_view, name='orders_year'),
url(r'^bills_week/$', bills_week_view, name='bills_week'),
url(r'^bills_month/$', bills_month_view, name='bills_month'),
url(r'^bills_year/$', bills_year_view, name='bills_year'),
url(r'^basket/$', basket_view, name='basket_view'),
url(r'^cart_true_view/$', cart_true_view, name='cart_true_view'),
url(r'^privat24/$', privat24, name='privat24'),
url(r'^bill/(?P<pk>[\d]+)/$',
BillDetail.as_view(),
name='bill_detail'),
]
просмотров
class JointProductsDetail(DetailView):
model = JointProduct
http_method_names = ['get', 'post']
def get(self, request, *args, **kwargs):
if request.is_ajax():
def post(self, request, *args, **kwargs):
if self.request.is_ajax():
def order_form_valid(self, form, order):
def form_valid(self, form, order, object_item):
def add_to_cart_view(request):