views:

24

answers:

1

I'm using a modelform to display only a subset of the fields in the model. When the form is submitted it fails form.is_valid() but form.errors is empty. I'd rather not display all my code here, but below is a sample:

Model and Form

class Videofiles(models.Model):
    active = models.CharField(max_length=9)
    filenamebase = models.CharField(max_length=180, primary_key=True, db_column='FilenameBase')
    program = models.CharField(max_length=60, db_column='Program') 
    displayname = models.CharField(max_length=150, db_column='DisplayName') 
    description = models.TextField(db_column='Description', blank=True) 
    tagskeywords = models.TextField(db_column='TagsKeywords', blank=True) 

    class Meta:
        db_table = u'legacyTable'

class VideoFilesForm(ModelForm):
    filenamebase = forms.CharField(max_length=30)
    displayname = forms.CharField(max_length=30)
    description = forms.CharField(max_length=30, required=False)
    tagskeywords = forms.CharField(max_length=60, required=False)

    class Meta:
        model=Videofiles
        fields=['filenamebase','displayname','description','tagskeywords']

View

def editClip(request, clipId):
    clip = Videofiles.objects.get(filenamebase=clipId)
    form = VideoFilesForm(instance=clip)
    if request.method == 'POST':
        if 'save' in request.POST:
            if form.is_valid():
                form.save()
            else:
                print form.errors
    return render_to_response('legacyDB/edit_clip.html',locals())
+2  A: 

Your form is unbound, because you are not passing any data to it. Calling is_valid on an unbound form will always return False, with empty errors (see the form api docs).

Your view should be something like the following:

def editClip(request, clipId):
    clip = Videofiles.objects.get(filenamebase=clipId)
    if request.method == 'POST':
        # create bound form with input from request.POST
        form = VideoFilesForm(request.POST, instance=clip)
        if 'save' in request.POST:
            if form.is_valid():
            form.save()
        else:
            print form.errors
    else:
        # create unbound form to display in template
        form = VideoFilesForm(instance=clip)
            return render_to_response('legacyDB/edit_clip.html',locals())
Alasdair
Thanks... figured that out after I walked away from it for a bit. I'm an idiot.
jamida