这很奇怪,因为当我将一个部分的相同代码复制到另一个部分时,我不知道为什么会返回此错误,只更改一些通用视图(UpdateView)的标题,并且从不执行 get 函数以返回我的表单进行编辑一个客户,这就是代码:
网址
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
,我可以检索员工编辑的表单,但我不能为客户做同样的事情,我错过了一些东西我现在没注意到?这让我发疯了!有什么想法吗?
问候!