On Nov 7, 1:53 pm, Jonathan Buchanan <[EMAIL PROTECTED]>
wrote:
> [EMAIL PROTECTED] wrote:
> > Here's what I've done:
>
> > if request.method == 'POST':
> >         submission = request.POST.copy()
> >         submission['slug'] = slugify(submission['name'])
> >         form = ClubFormClass(submission)
> >         if Club.objects.get(slug=submission['slug']):
> >           form.errors['name'] = '<ul><li>Club is already in database</
> > li></ul>'
> >         if form.is_valid():
> >                    form.save()
> >                    return HttpResponseRedirect('/thankyou/')
> >     else:
> >         form = ClubFormClass()
>
> I'd implement it as a clean_fieldname method. The fact that in the above
> you're having to mess around with errors dictionary manually for
> something this simple is a bad smell to watch out for.
>
> form
> ----
>
> def ClubForm(forms.Form):
>      name = forms.CharField(...)
>      ...
>
>      def clean_name(self):
>         slug = slugify(self.cleaned_data['name'])
>          if Club.objects.filter(slug=slug).count():
>              raise forms.ValidationError(u'Club is already in database')
>         return self.cleaned_data['name']
>
> view
> ----
>
> if request.method == 'POST':
>      form = ClubForm(data=request.POST)
>      if form.is_valid():
>          club = form.save() # Do the slugify() in your save method
>          return HttpResponseRedirect(club.get_absolute_url())
> else:
>      form = ClubForm()
>
> Jonathan.

Thanks Jonathan... yeah, I smelled that smell. I'm not quite following
you, though. Where is clean_name() called? I think I may be confused
because I have     ClubFormClass = nforms.form_for_model(Club)  rather
than def ClubForm(forms.Form):


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to