0

I have difficulty saving my form into db. Every time I click on save it didn't save in db/admin. Can't seem to figure it out. I hope somebody out there can help me with this.

This project is about collecting information on a new customer for a construction company and if the customer exist in the db then it will view the detail of the particular customer and be able to edit the particular customer when needed and be able to overwrite the data when save. This information gather from a customer model and building model and customerForm and buildingForm and display in customer template.

models.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)

forms.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(),
                  }

views.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))

customer.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>   

I have try this demo on multiple model entry in single form http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/ and Django: multiple models in one template using forms and How to use two different Django Form at the same template? but can't see where the mistake is. My task is quite simple for now, I just have to save all the information that has been keyed-in and saved in the database.

Community
  • 1
  • 1
noobes
  • 161
  • 2
  • 18

1 Answers1

0

You probably have a validation error somewhere. Since you're not displaying any of the form errors in the template, your form will just be redisplayed with no explanation.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • thks for replying. I think you are right. That is my problem, I can't see any error and I don't know where to start from and test. When I click save on html it seem like refresh but when i check on db/admin nothing happen...but when I key in from admin then i can take the data out to my html...:( can you help me on what to check or look into??thank u – noobes Apr 03 '13 at 10:58