On Tue, Jun 23, 2009 at 9:04 PM, db_333 <pleu...@gmail.com> wrote:

>
> Hi I'm trying to figure out why a record I am trying to save from a
> form will not commit.  The form seems to be working fine, and I wanted
> to know if there were any specific debugging statements I could use to
> see where this is failing.
>
> The following is what I have:
>
> 1. model:
>
> class Units(models.Model):
>    serial_number = models.CharField(max_length=25, primary_key=True)
>    build = models.CharField(max_length=50, blank=True)
>    model = models.CharField(max_length=20, blank=True)
>    create_date = models.DateField(null=True, blank=True)
>    source = models.CharField(max_length=20, blank=True)
>    class Meta:
>        db_table = u'units'
>
> 2. form:
>
> class UnitFormSave(forms.Form):
>        serial_number = forms.CharField(max_length=50)
>        build = forms.CharField(max_length=50)
>        model = forms.CharField(max_length=10)
>        source = forms.CharField(max_length=10)
>
> 3. view:
>
> def save_unit(request):
>        if request.method == 'POST':
>                form = UnitFormSave(request.POST)
>                if form.is_valid():
>                        unit = Units.objects.create(
>                        serial_number = form.cleaned_data['serial_number'],
>                        build = form.cleaned_data['build'],
>                        model = form.cleaned_data['model'],
>                        source = form.cleaned_data['source'])
>                        unit.save()
>                        return HttpResponseRedirect('/search/')
>                else:
>                        form = UnitFormSave()
>
>                variables = RequestContext(request, {
>                        'form': form
>                })
>                return render_to_response('unit_save.html', variables)
>

Is this actually the way you have the save_unit function indented? You've
got everything indented under "if request.method == 'POST'" with nothing for
the GET case.  It rather looks like from the "else" currently paired with
"if form.is_valid():" through the end of the funciton should be out-dented
one level.  That way if form is not valid what will happen is that the form
annotated with error messages about what is incorrect will be re-displayed.
What is happening with the code you have posted is that the error-annotated
form is being tossed away and replaced with a new blank form.  As it is,
though, I don't see how you are getting anything valid on a GET to begin
with, so I am not even sure this is the code you are actually running.

Karen

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

Reply via email to