指定初始数据时不保存表单

时间:2018-04-07 07:36:02

标签: python django

以下表格无法保存,我也不知道原因。目标只是获取初始数据,以便用户不必再次输入,并在表单完成后将其保存到模型中。

views.py

class GetSellData(View):
    form_class = MakeSale
    template_name = 'laptops/laptop_sale.html'

    def post(self, request, *args, **kwargs):
        #Get post data from 'laptopname' button
        laptopid = request.POST.get('laptopname')
        #Store laptopid for use somewhere else
        request.session['laptopid'] = laptopid
        #get matching laptop id
        laptop = Laptop.objects.get(id=laptopid)
        #specify intital data based on laptop id for pre-populating the form
        data = {'Name': laptop.Name, 'specification': laptop.specification, 'warranty': laptop.warranty,'condition':laptop.condition}
        form = self.form_class(initial=data)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()
            return render(request,'laptops/laptop_list.html')
        else:
            return render(request, self.template_name, {'form': form})'

0 个答案:

没有答案
相关问题