models.py
class Author(models.Model):
author_id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
age=models.IntegerField()
class Meta:
db_table=u'Author'
def __unicode__(self):
return u"%d %s %s %s %d" % (self.pk, self.first_name, self.last_name, self.email,self.age)
class Book(models.Model):
book_id=models.AutoField(primary_key=True,unique=True)
book_name=models.CharField(max_length=30)
publisher_name=models.CharField(max_length=40)
author=models.ForeignKey(Author)
class Meta:
db_table = u'Book'
def __unicode__(self):
return u'%d %s %s' % (self.pk, self.book_name, self.publisher_name)
my forms.py
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ['author_id','first_name','last_name','email','age']
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields=['book_id','book_name','publisher_name','author_id']
views.py
def addbook(request):
log.debug("test....")
form = BookForm
if request.POST:
form = BookForm(request.POST)
if form.is_valid():
form.save()
return render_to_response('addbook.html',{'form': form} , context_instance=RequestContext(request))
我试图将数据插入数据库并通过模板显示它。但我不确定我使用的代码是正确的因为我得到了很多错误。最近我得到错误为“ValueError:ModelForm没有指定模型类。“。
但在纠正此错误后,错误即将到来,无法将数据保存到数据库。
请检查代码是否正确,如果没有给我正确的代码做同样的事情。我认为在views.py中有一些问题。请检查一切并让我得到合作。
感谢。