运行以下表单时出现错误。请帮助我修复错误。
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class student(models.Model):
name = models.CharField(max_length=50)
emailid = models.EmailField(max_length=60)
marks = models.CharField(max_length=11)
date = models.DateTimeField()
def __str__(self):
return self.name
forms.py
from django import forms
from .models import *
class student_form(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(), required=True, max_length=100)
emailid = forms.EmailField(widget=forms.EmailField(), required=True)
class Meta():
model = student
fields = ['name','emailiid']
我尝试了很多事情,但是没有解决方案。请查看此代码,以帮助解决该问题。因为我是Django的新手,所以让我感到困惑。
答案 0 :(得分:2)
在forms.py
中,您必须缩进Meta
类,使其成为student_form
类的一部分。
这给出了:
from django import forms
from .models import *
class student_form(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(), required=True, max_length=100)
emailid = forms.EmailField(widget=forms.EmailField(), required=True)
class Meta:
model = student
fields = ['name','emailiid']