Thursday, October 5, 2017

Re: Problm using FileField with inline formsets (inlineformset_factory)

You saved my ass, thanks!!!

El miércoles, 10 de septiembre de 2008, 13:46:20 (UTC+4), Daniel Roseman escribió:
On Sep 10, 9:50 am, "c.poll...@bangor.ac.uk" <c.poll...@bangor.ac.uk>
wrote:
> Hi
>
> I'm having a problem with inline formsets containing file field, i've
> had a good google but couldn't find an answer, so apolgies if this is
> a stupid or repetative question.
>
> I have a model Applicatant, which has a one to many relation with
> Education which contains a filefeild. My problem is that when django
> comes to validate the inline form education it reports that the field
> is empty, if i tell the model to allow empty feilds then the file is
> not saved
>
> Is it possible to use file fields with the inlineformset_factory or do
> i need to go at this another way?
>
> I have included my code, pretty simple and pretty much copied from the
> documentation:
>
> def myview(request):
>     InlineFormSet = inlineformset_factory(Applicant, Education)
>     applicant = Applicant.objects.get(pk=1)
>     if request.method == 'POST':
>         formset = InlineFormSet(request.POST, request.FILES,
> instance=applicant)
>         if formset.is_valid():
>            Do stuff
>    else:
>        formset = InlineFormSet(instance=applicant)
>
>    return render_to_response("mytemplate.html", { "formset":
> formset,})
>
> where mytemplate.html is simply
>
> <form action="./do" method="POST">
>
>     {{ formset.management_form }}
>     <table>
>         {% for form in formset.forms %}
>         {{ form.as_ul }}
>         {% endfor %}
>     </table>
>
>     <input type="submit" name="next" value="Next">
> </form>
>
> Many than ks
> Charlotte


The problem isn't with Formsets, it's just that you haven't set your
enctype in the HTML form element. It should be:
<form enctype="multipart/form-data" action="./do" method="POST">

See http://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-files-to-a-form
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87f723fa-9938-4472-b53e-a03111f0c946%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment