ModelForm没有出现在Django模板中?

车型

class VideoInfo(models.Model): 

user = models.ForeignKey(User)

video_name = models.CharField(max_length=200)

director = models.CharField(max_length=200)

cameraman = models.CharField(max_length=200)

editor = models.CharField(max_length=200)

reporter = models.CharField(max_length=200)

tag = models.TextField()

形式

class LoginForm(forms.Form): 

username = forms.CharField(max_length=50)

password = forms.CharField(widget=PasswordInput())

class VideoInfoForm(forms.Form):

class Meta:

model = VideoInfo

fields = ['video_type', 'director', 'cameraman', 'editor', 'reporter', 'tag']

浏览:

class Main(View): 

'''Index page of application'''

def get(self, request):

model = VideoInfo

form = VideoInfoForm()

return render_to_response('main.html', {'form':form}, context_instance=RequestContext(request))

模板调用为:ModelForm没有出现在Django模板中?

{{form.as_p}} 

形式没有显示出来,但如果我用LoginForm它显示出来。我究竟做错了什么?

回答:

变化:

class VideoInfoForm(forms.Form): 

要:

class VideoInfoForm(forms.ModelForm): 

回答:

当你要使用模型的形式,你的窗体的定义是不正确的。

变化

class VideoInfoForm(forms.Form): 

class VideoInfoForm(forms.ModelForm): 

# ------------------^ use ModelForm not Form

旁注:

代替fields使用一长串的exclude只列出不需要的领域。

class VideoInfoForm(forms.ModelForm): 

class Meta:

model = VideoInfo

exclude = ['user',]

以上是 ModelForm没有出现在Django模板中? 的全部内容, 来源链接: utcz.com/qa/261978.html

回到顶部