Django ModelForm is giving me a validation error that doesn't make sense
Posted
by
River Tam
on Stack Overflow
See other posts from Stack Overflow
or by River Tam
Published on 2012-12-05T00:53:00Z
Indexed on
2012/12/05
5:04 UTC
Read the original article
Hit count: 353
I've got a ModelForm based on a Picture.
class Picture(models.Model):
name = models.CharField(max_length=100)
pub_date = models.DateTimeField('date published')
tags = models.ManyToManyField('Tag', blank=True)
content = models.ImageField(upload_to='instaton')
def __unicode__(self):
return self.name
class PictureForm(forms.ModelForm):
class Meta:
model = Picture
exclude = ('pub_date','tags')
That's the model and the ModelForm, of course.
def submit(request):
if request.method == 'POST': # if the form has been submitted
form = PictureForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/django/instaton')
else:
form = PictureForm() # blank form
return render_to_response('instaton/submit.html', {'form': form}, context_instance=RequestContext(request))
That's the view (which is being correctly linked to by urls.py)
Right now, I do nothing when the form submits. I just check to make sure it's valid. If it is, I forward to the main page of the app.
<form action="/django/instaton/submit/" method="post"> {% csrf_token %}
{{ form.as_p }}
<input type="submit" value"Submit" />
</form>
And there's my template (in the correct location).
When I try to actually fill out the form and just validate it, even if I do so correctly, it sends me back to the form and says "This field is required" between Name and Content. I assume it's referring to Content, but I'm not sure.
What's my problem? Is there a better way to do this?
© Stack Overflow or respective owner