0

我很难将表格保存到数据库中。每次我点击保存时,它都没有保存在 db/admin 中。似乎无法弄清楚。我希望有人可以帮助我解决这个问题。

该项目是关于为建筑公司收集新客户的信息,如果客户存在于数据库中,那么它将查看特定客户的详细信息,并能够在需要时编辑特定客户,并能够在需要时覆盖数据节省。此信息从客户模型和建筑模型以及 customerForm 和 buildingForm 收集并显示在客户模板中。

模型.py

class customer(models.Model)
    F_NAME  = models.CharField(max_length = 50) 
    L_NAME  = models.CharField(max_length = 50)
    ADD     = models.CharField(max_length = 60, blank =True)
    EMAIL   = models.EmailField()
    def __unicode__(self):
        return '%s' % ( self.F_NAME )

class building(models.Model):
    CUSTOMER      = models.ForeignKey(customer)
    B_USE         = models.CharField(max_length = 2, blank = True, choices = c.Use)
    B_FLOORSPACE  = models.IntegerField(default=0)
    B_YEAR        = models.IntegerField(null = True, blank = True)
    B_TYPE        = models.CharField(max_length = 2, blank = True, choices = c.Type)
    def __unicode__(self):
        return '%s' % (self.B_USE)

表格.py

class customerForm(ModelForm):
    F_NAME = forms.CharField(widget=forms.TextInput(attrs={'size':'34'}))
    L_NAME = forms.CharField(widget=forms.TextInput(attrs={'size':'34'}))  
    EMAIL  = forms.CharField(widget=forms.TextInput(attrs={'size':'19'}))  
    ADD    = forms.CharField(widget=forms.TextInput(attrs={'size':'34'}))
    class Meta:
        model = customer

class buildingForm(ModelForm):
    CUSTOMER     = forms.CharField(widget=forms.TextInput(attrs={'size':'20'}))
    B_FLOORSPACE = forms.CharField(widget=forms.TextInput(attrs={'size':'4'}))
    B_YEAR       = forms.CharField(widget=forms.TextInput(attrs={'size':'4'})) 
    class Meta:
        model = building
        exclude = ('CUSTOMER',)
        widgets = {'B_USE'       : RadioSelectNotNull(),
                   'B_TYPE'      : RadioSelectNotNull(),
                  }

视图.py

def customerView(request ,**kwargs ):
    context ={}
    try:
        this_customer = customer.objects.get(id = kwargs ['pk'])
    except:
        this_customer = customer.objects.create(id = kwargs['pk'])
    try: 
        bform = buildingForm(instance=building())
        context['build']=True
        context['bform']=bform
    except: 
        context['build']=False
    form = customerForm(instance=this_customer)
    context['form']= form

    if request.method == 'POST':
        form = customerForm(request.POST, instance = customer())
        bform = [buildingForm(request.POST, prefix = str(x), instance = building()) for x in range (0,3)]
        if form.is_valid() and all ([bf.is_valid() for bf in bform]):
            new_customer = form.save()
            for bf in bform:
                new_build = bf.save(commit = False)
                new_build.CUSTOMER = new_customer
                new_build.save()
            return HttpResponseRedirect('customer.html')
    else:
        form = customerForm(instance=customer())
        bform = [buildingForm(prefix=str(x), instance=building())for x in range(0,3)]

    return render_to_response('customer.html',{'customer_form':form, 'build_form':bform}, context_instance = RequestContext(request))

客户.html

<form action="" method="post">
<button type="submit" name="customer">Save</button>  
  {% csrf_token %}
    {{ form.id }} 

 ...more code...
<table> 
  <tr><td><div>First Name</div>{{ form.F_NAME }}</td></tr>     
  <tr><td><div>Last Name</div>{{ form.L_NAME }}</td></tr>
</table>

 ....more code....

{% if build %}

 ...more code....
<table> 
  <tr><td><div>Build Use</div></td><td>{{ bform.B_USE }}</td>
      <td><div>Build Space</div></td><td>{{ bform.B_FLOORSPACE }}</td>
      </tr>

 ...more code... 
</form>   

我已经在单一形式的多个模型条目上尝试了这个演示 http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/Django:一个模板中的多个模型使用表单 以及 如何在同一模板中使用两个不同的 Django 表单? 但看不到错误在哪里。我现在的任务很简单,我只需要保存所有已键入并保存在数据库中的信息。

4

1 回答 1

0

您可能在某处遇到验证错误。由于您没有在模板中显示任何表单错误,因此您的表单只会重新显示而没有任何解释。

于 2013-04-03T10:12:10.340 回答