简体   繁体   中英

ModelForm not showing up in Django template?

models

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

forms

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

Views:

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

Calling in template as:

{{form.as_p}}

The form is not showing up but if I use LoginForm it's showing up. what am I doing wrong?

Change:

class VideoInfoForm(forms.Form):

To:

class VideoInfoForm(forms.ModelForm):

As you want to use model form, your definition of the form is not correct.

Change

class VideoInfoForm(forms.Form):

to

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

Sidenote:

Instead of long list of fields use exclude to just list fields that are not needed.

class VideoInfoForm(forms.ModelForm):
    class Meta:
        model = VideoInfo
        exclude = ['user',]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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