On Sep 4, 5:19 pm, Jagdeep Singh Malhi <singh.malh...@gmail.com>
wrote:
> I try to Create forms from models
> I am facing error message with 'is_valid()'
>
> error is  :
> {
> AttributeError at /add_db/
>
> 'Input' object has no attribute 'is_valid'
>
> Request Method:         POST
> Request URL:    http://localhost/django/add_db/
> Django Version:         1.2.1
> Exception Type:         AttributeError
> Exception Value:
>
> 'Input' object has no attribute 'is_valid'
>
> Exception Location:     /home/jagdeep/mysite/add_db/views.py in add_db,
> line 11
> Python Executable:      /usr/bin/python
> Python Version:         2.6.5
>
> }
>
> Coded file are given below:
> model.py :
> from django.db import models
> from django.forms import ModelForm
>
> class Input(models.Model):
>     input1 = models.FloatField()
>     input2 = models.FloatField()
>
> class Output(models.Model):
>         out = models.ForeignKey(Input)
>         output = models.FloatField()
>
> class InputForm(ModelForm):
>     class Meta :
>         model = Input
>
> class OutputForm(ModelForm):
>     class Meta :
>         model = Output
>
> View.py :
> from django.http import HttpResponseRedirect
> from django.shortcuts import render_to_response
> from mysite.add_db.models import *
> from django.template import RequestContext
> from django.core.urlresolvers import reverse
>
> def add_db(request):
>     if request.method == 'POST':
>         form = Input(request.POST)
>         if form.is_valid():
>             cd = form.cleaned_data
>             input1 = cd['input1']
>             input2 = cd['input2']
>             form.save()
>             output = input1 + input2
>
>             return render_to_response('add_db/output.html', {'form':
> form, 'input1':input1, 'input2':input2, 'output':output},
> context_instance=RequestContext(request))
>     else:
>         form = Input()
>     return render_to_response('add_db/add.html', {'form': form},
> context_instance=RequestContext(request))
>
> Please help.
>
> Thanks

Fairly obviously, you've instantiated the model - Input() - rather
than the form - InputForm() - in both branches of the if statement of
your view.
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to