Thanks to justinf on #django for pointing out that I need to pass in
request.FILES in my formset. If you're working with file fields (and
image fields), the parameter is essential. Below is my current
working view for future references.
-------------------------------------------------------------------------------------------------------------------
def add_post(request):
section_id = int(request.GET.get('s', 0))
category_id = int(request.GET.get('c', 0))
PostImageFormSet = \
inlineformset_factory(Post, PostImage, form=PostImageForm,
max_num=4, extra=4)
if request.method == 'POST':
form = PostForm(request.POST)
formset = PostImageFormSet(request.POST, request.FILES)
if form.is_valid() and formset.is_valid():
new_post = form.save(commit=False)
new_post.poster = request.user
new_post.save()
formset.instance = new_post
formset.save()
return HttpResponseRedirect(new_post.get_absolute_url())
else:
form = PostForm()
formset = PostImageFormSet()
return render_to_response('classified/post_form.html',
{ 'form': form,
'formset': formset,
'add': True,
'section_id': section_id,
'category_id': category_id,
'categories': Category.objects.all(),
'filtered_section':
filtered_section },
context_instance=RequestContext(request))
-------------------------------------------------------------------------------------------------------------------
On Sep 2, 9:30 pm, Aaron <[EMAIL PROTECTED]> wrote:
> I figured it out. The problem was that I forgot to include
>
> {{ formset.management_form }}
>
> in my template.
>
> But now I have a new problem. After validation, the post gets saved
> but not the image.
>
> On Sep 2, 5:08 pm, Aaron <[EMAIL PROTECTED]> wrote:
>
> > Hi, has anyone encounter a similar error?
>
> > ValidationError at /post/add/
> > [u'ManagementForm data is missing or has been tampered with']
>
> > I am able to display the inline form, but the error occurs when I try
> > to pass the request.POST data into the PostImageFormSet
>
> > Below is my view:
> > -----------------------------------------------------------------------------------------------------------------------
> > def add_post(request):
>
> > section_id = int(request.GET.get('s', 0))
> > category_id = int(request.GET.get('c', 0))
>
> > PostImageFormSet = \
> > inlineformset_factory(Post, PostImage, max_num=1)
>
> > if request.method == 'POST':
> > form = PostForm(data=request.POST)
> > formset = PostImageFormSet(data=request.POST)
> > if form.is_valid() and formset.is_valid():
> > new_post = form.save(commit=False)
> > new_post.poster = request.user
> > new_post.save()
> > formset.instance = new_post
> > formset.save()
> > return HttpResponseRedirect(new_post.get_absolute_url())
> > else:
> > form = PostForm()
> > formset = PostImageFormSet()
>
> > return render_to_response('classified/post_form.html',
> > { 'form': form,
> > 'formset': formset,
> > 'add': True,
> > 'section_id': section_id,
> > 'category_id': category_id,
> > 'categories':
> > Category.objects.all() },
>
> > context_instance=RequestContext(request))
>
> > -----------------------------------------------------------------------------------------------------------------------
>
> > Any helpful info regarding this error will be greatly appreciated.
>
> > Aaron
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---