1

楷模

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

调用模板为:

{{form.as_p}}

表格没有出现,但如果我使用LoginForm它就会出现。我究竟做错了什么?

4

2 回答 2

2

改变:

class VideoInfoForm(forms.Form):

至:

class VideoInfoForm(forms.ModelForm):
于 2013-08-02T07:10:33.023 回答
1

由于您要使用模型表单,因此您对表单的定义不正确。

改变

class VideoInfoForm(forms.Form):

class VideoInfoForm(forms.ModelForm):
#       ------------------^ use ModelForm not Form

边注:

fields而不是使用长列表exclude来仅列出不需要的字段。

class VideoInfoForm(forms.ModelForm):
    class Meta:
        model = VideoInfo
        exclude = ['user',]
于 2013-08-02T07:10:43.737 回答