繁体   English   中英

ModelForm没有出现在Django模板中?

[英]ModelForm not showing up in Django template?

楷模

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它就会显示出来。 我究竟做错了什么?

更改:

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',]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM