Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » FormSet validation - not sure how to implement [RSS Feed]

#1 Dec. 21, 2010 13:05:32

Michael T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

FormSet validation - not sure how to implement


I am using 2 formsets on a template form and it shows fine but I don't
know how to implement the form validation. Not allow a field to be
empty.
I have tried the basic method as documented but it doesn't trigger any
messages.
However, I also haven't put any defined errors anywhere so I don't
know how an error message would appear.
Bottom line is I don't understand the process and therefore can't set
it up properly.
If someone could show how to not allow the field
{{ AddShirtFormSet.form.madeIn }} to NOT be empty, that would be a big
help - thanks

def addShirt(request):
AddShirtFormSet =
modelformset_factory(Shirt,exclude=('create_date',))
AddUserShirtFormSet =
modelformset_factory(UsersShirt,exclude=('shirt',))
if request.method == 'POST':
AddShirtFormSet = AddShirtFormSet(request.POST, request.FILES,
prefix = 'shirt')
AddUserShirtFormSet = AddUserShirtFormSet(request.POST,
request.FILES,prefix = 'usershirt')
if AddShirtFormSet.is_valid():
#formset.save()
# do something.
return HttpResponseRedirect('/shirt/') # Redirect after
POST
else:
AddShirtFormSet = AddShirtFormSet(prefix = 'shirt')
AddUserShirtFormSet = AddUserShirtFormSet(prefix =
'usershirt')

return render_to_response("shirt/addshirt.html",
RequestContext(request,{'AddShirtFormSet':
AddShirtFormSet,'AddUserShirtFormSet': AddUserShirtFormSet,'error':
True,}))

*********** In template ****************
<form name='AddShirt' method="post" action="/shirt/add/">
{{ form.non_field_errors }}
{% csrf_token %}
{{ AddShirtFormSet.management_form }}
{{ AddUserShirtFormSet.management_form }}
<div id="addFrom">

<div class="fieldWrapper">
{{ AddShirtFormSet.form.madeIn.errors }}
<label for="madeIn">Country made in?:</label>
{{ AddShirtFormSet.form.madeIn }}
</div>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 22, 2010 11:43:36

Michael T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

FormSet validation - not sure how to implement


Never mind - I changed it to ModelForm class instead.
>From what I have read, FormSet is not designed to be used in the
generic form way.
Rather for bulk adds or something like that.
ModelForm works nice and easy.


On Dec 21, 7:04 am, Michael Thamm <webmas...@morningcat.com> wrote:
> I am using 2 formsets on a template form and it shows fine but I don't
> know how to implement the form validation. Not allow a field to be
> empty.
> I have tried the basic method as documented but it doesn't trigger any
> messages.
> However, I also haven't put any defined errors anywhere so I don't
> know how an error message would appear.
> Bottom line is I don't understand the process and therefore can't set
> it up properly.
> If someone could show how to not allow the field
> {{ AddShirtFormSet.form.madeIn }} to NOT be empty, that would be a big
> help - thanks
>
> def addShirt(request):
>     AddShirtFormSet =
> modelformset_factory(Shirt,exclude=('create_date',))
>     AddUserShirtFormSet =
> modelformset_factory(UsersShirt,exclude=('shirt',))
>     if request.method == 'POST':
>         AddShirtFormSet = AddShirtFormSet(request.POST, request.FILES,
> prefix = 'shirt')
>         AddUserShirtFormSet = AddUserShirtFormSet(request.POST,
> request.FILES,prefix = 'usershirt')
>         if AddShirtFormSet.is_valid():
>             #formset.save()
>             # do something.
>             return HttpResponseRedirect('/shirt/') # Redirect after
> POST
>     else:
>         AddShirtFormSet = AddShirtFormSet(prefix = 'shirt')
>         AddUserShirtFormSet = AddUserShirtFormSet(prefix =
> 'usershirt')
>
>     return render_to_response("shirt/addshirt.html",
> RequestContext(request,{'AddShirtFormSet':
> AddShirtFormSet,'AddUserShirtFormSet': AddUserShirtFormSet,'error':
> True,}))
>
> *********** In template ****************
> <form name='AddShirt' method="post" action="/shirt/add/">
>   {{ form.non_field_errors }}
>   {% csrf_token %}
>   {{ AddShirtFormSet.management_form }}
>   {{ AddUserShirtFormSet.management_form }}
>   <div id="addFrom">
>
>     <div class="fieldWrapper">
>         {{ AddShirtFormSet.form.madeIn.errors }}
>         <label for="madeIn">Country made in?:</label>
>         {{ AddShirtFormSet.form.madeIn }}
>     </div>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » FormSet validation - not sure how to implement [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support