def customer_add(request): """编辑客户""" if request.method == 'GET': form = CustomerForm() return render(request, 'web/customer_edit.html', {'form': form}) form = CustomerForm(data=request.POST) if form.is_valid(): form.save() return redirect('/customer/list/') return render(request, 'web/customer_edit.html', {'form': form})
def customer_add(request): """编辑客户""" if request.method == 'GET': form = CustomerForm() return render(request, 'web/form_submit.html', context=locals()) form = CustomerForm(data=request.POST) if form.is_valid(): form.save() return redirect('/web/customer/list/') return render(request, 'web/form_submit.html', context=locals())
def customer_edit(request, cid): """新增客户""" obj = models.Customer.objects.get(id=cid) if request.method == 'GET': form = CustomerForm(instance=obj) return render(request, 'web/customer_add.html', {'form': form}) form = CustomerForm(data=request.POST, instance=obj) if form.is_valid(): form.save() return redirect('/customer/list/') return render(request, 'web/customer_add.html', {'form': form})
def customer_add(request): ''' 添加客户 :param request: :return: ''' form = CustomerForm() if request.method == 'POST': form = CustomerForm(data=request.POST) if form.is_valid(): form.save() return redirect('/customer/list') return render(request, 'customer_add.html', {'form': form})
def customer_add(request): """ 新增客户 :return: """ if request.method == 'GET': form = CustomerForm() return render(request, 'customer_add.html', {'form': form}) form = CustomerForm(data=request.POST) if form.is_valid(): form.save() return redirect(reverse('customer-list')) return render(request, 'customer_add.html', {'form': form})
def customer_edit(request, cid): ''' 编辑客户 :param request: :param cid: :return: ''' obj = models.Customer.objects.filter(id=cid).first() form = CustomerForm(instance=obj) #注意,这里必须写成CustomerForm(instance=obj)(看源码方法要求), 如果写成位置参数 CustomerForm(obj) 会报错 ''Customer' object has no attribute 'get'' if request.method == 'POST': form = CustomerForm(data=request.POST,instance=obj) #注意,这里必须是一个关键字参数,否则,如果这样写CustomerForm(request.POST,obj),form.save是增加记录,而不是修改 if form.is_valid(): form.save() return redirect('customer/list') return render(request, 'customer_edit.html', {'form': form})
def customer_edit(request, cid): """ 编辑客户 :param cid: 客户id :return: """ obj = Customer.objects.get(id=cid) if request.method == 'GET': form = CustomerForm(instance=obj) return render(request, 'customer_edit.html', {'form': form}) form = CustomerForm(data=request.POST, instance=obj) if form.is_valid(): form.save() return redirect(reverse('customer-list')) return render(request, 'customer_edit.html', {'form': form})
def customer_add(request): """ 添加客户 :param request: :return: """ if request.method == 'GET': # 把表单渲染到html页面 form = CustomerForm() return render(request, 'customer_add.html', {'form': form}) form = CustomerForm(data=request.POST) if form.is_valid(): form.save() return redirect('/customer/list/') return render(request, 'customer_add.html', {'form': form})
def customer_edit(request, cid): """ 修改客户 :param request: :return: """ try: obj = models.Customer.objects.get(id=cid) except Exception as e: print('修改客户id不存在:', e) if request.method == 'GET': form = CustomerForm() return render(request, 'customer_add.html', {'form': form}) form = CustomerForm(data=request.POST, instance=obj) if form.is_valid(): form.save() return redirect('/customer/list/') return render(request, 'customer_edit.html', {'form': form})