在ajax调用django泛型视图时,XHR状态返回0

时间:2013-07-21 20:02:03

标签: ajax django django-views

这很奇怪,因为我不知道为什么在将一个部分的相同代码复制到另一个部分时返回此错误,只更改某些通用视图的标题(UpdateView)并且从不执​​行get函数以返回my用于编辑单个客户的表单,即代码:

网址

from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
from . import forms
from . import invoice


urlpatterns = patterns('',
    url(r'^pedidos/',views.PedidosListView.as_view(),name="pedidos_list"),
    url(r'^pedidos/add',views.add_pedido, name="pedido_add"),
    url(r'^pedidos/edit/(?P<pedido_id>\d+)',views.edit_pedido, name="pedido_edit"),
    url(r'^pedidos/delete/(?P<pedido_id>\d+)',views.delete_pedido, name="pedido_delete"),
    url(r'^pedido/(?P<pk>\d+)',forms.DetailPedido.as_view(), name="pedido_detail"),
    url(r'^pedido-pdf/(?P<pk>\d+)',invoice.detalle_pedido_pdf, name="pedido_detail_pdf"),
    url(r'^clientes/',views.ClientesListView.as_view(),name="clientes_list"),
    url(r'^clientes/edit/(?P<pk>\d+)$',forms.ClienteUpdateView.as_view(), name="clientes_edit"),
    url(r'^empleados/edit/(?P<pk>\d+)$',forms.EmpleadoUpdateView.as_view(),name="edit"),
    url(r'^empleados/',views.EmpleadoListView.as_view(),name="list"),

)

形式

class ClienteUpdateView(UpdateView):
    form_class = ClienteModelForm
    model = Cliente
    template_name = 'ventas/form.html'

    def get(self, request, **kwargs):
        self.object = Cliente.objects.get(pk=self.kwargs['pk'])
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        context = self.get_context_data(object=self.object, form=form)
        return self.render_to_response(context)

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.save()
        return HttpResponseRedirect('ventas/clientes_list.html')

    def form_invalid(self,form):
      if self.request.is_ajax():
          return HttpResponseBadRequest(json.dumps(form.errors),
                                       mimetype="application/json")

的javascript

$(".edit").click(function(ev){
    ev.preventDefault();
    var url = $(this).data('form');
    $.ajax({
        url: url,
        success: function(data, status){
            $('body').append($('<div id="myModal" class="modal hide fade">' + data + '</div>').modal());
            $("#myModal").find(".datePicker" ).datepicker({ dateFormat: "dd/mm/yy" });
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr.status);
            console.log(xhr.responseText);
            console.log(thrownError);
        }
    })

    return false; // prevent the click propagation
});

HTML

  <button data-form="{% url 'ventas:clientes_edit' c.id %}" class="edit btn btn-mini btn-info">

                                                

如果在ajax url中更改'ventas / empleados / edit / someid'的值(与我为do ClienteUpdateView复制的相同,我可以检索员工编辑的表单但我不能这样做对于顾客来说,我错过了一些我现在无法注意到的东西?这让我疯狂!有什么想法吗?

问候!

0 个答案:

没有答案