我有一个CustomerAddForm和一个CustomerOverview HTML表。当我在CustomerAddForm中添加新记录时,它将显示在CustomerOverview HTML表中。我还具有通过单击客户名称从CustomerOverview HTML打开现有客户的功能。
当我打开现有客户时,它会打开客户,但是当我点击提交时,它会将已打开的客户保存到新记录中。如果我在视图中对客户ID进行硬编码,则会保存给定的ID。因此,在点击提交时看起来没有推送ID。
正常;
def customeradd(request, id = None)
使用硬编码ID;
def customeradd(request, id = 1)
我的观点.py;点击提交request.method =='POST'似乎总是如此。并且else语句永远不会运行。
def customeradd(request, id = None):
if id:
customer = Customer.objects.get(pk = id)
else:
customer = None
if request.method == 'POST':
form = CustomerAddForm(request.POST or None, instance = customer)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
messages.success(request, 'Customer added succesfully')
return HttpResponseRedirect('/customeroverview/')
else:
messages.error(request, 'Customer save error, please check fields below')
else:
form = CustomerAddForm(instance = customer)
if form.is_valid():
form.save()
messages.success(request, 'Customer edited succesfully')
return HttpResponseRedirect('/customeroverview/')
return render_to_response("customer-add.html",
{"customer_add_form": form},
context_instance=RequestContext(request))
my forms.py(django-crispy-form)
class CustomerAddForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(CustomerAddForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_action = '/customeradd/'
self.helper.add_input(Submit('submit', 'Submit'))
self.helper.add_input(Button('cancel', 'Cancel'))
class Meta:
model = Customer
my urls.py
url(r'^customeroverview/$', 'customer.views.customeroverview', name='customeroverview'),
url(r'^customeradd/$', 'customer.views.customeradd', name='customeradd'),
url(r'^customeradd/(?P<id>\w+)$', 'customer.views.customeradd', name='customeredit'),
客户add.html
{% block content %}
{% load crispy_forms_tags %}
{% crispy customer_add_form customer_add_form.helper %}
{% endblock %}
答案 0 :(得分:1)
使用它:
def customeradd(request, id=None):
if id:
customer = Customer.objects.get(pk = id)
else:
customer = Customer()
if request.method == 'POST':
form = CustomerAddForm(request.POST, instance=customer)
if form.is_valid():
form.save()
messages.success(request, 'Customer added succesfully')
return HttpResponseRedirect('/customeroverview/')
else:
messages.error(request, 'Customer save error, please check fields below')
else:
form = CustomerAddForm(instance = customer)
return render_to_response("customer-add.html",
{"customer_add_form": form},
context_instance=RequestContext(request))
并替换
self.helper.form_action = '/customeradd/'
与
self.helper.form_action = ''
在forms.py
答案 1 :(得分:0)
您可以使用特定功能编辑客户:
@require_POST
@csrf_protect
def customeredit(request, id=None):
edited_customer = CustomerAddForm(request.POST)
if edited_customer.is_valid():
customer = edited_customer.save(commit=False)
customer.id = id
customer.save()
messages.success(request, 'Customer edited succesfully')
return HttpResponseRedirect('/customeroverview/')
else:
messages.error(request, 'Customer save error, please check fields below')
return render_to_response("customer-add.html",
{"customer_add_form": edited_customer},
context_instance=RequestContext(request))
这意味着您需要修改urls.py
url(r'^customeredit/(?P<id>\w+)$', 'customer.views.customeraedit', name='customeredit'),