abolotnov
abolotnov

Reputation: 4332

Django form object has no attribute issue

I have a simple form and am trying to POST it to a simple service.

The form model:

class order_form(forms.Form):
    name = forms.CharField(max_length=100)
    phone = forms.CharField(max_length=20)
    email = forms.CharField(max_length=100)
    address = forms.CharField(max_length=200)
    ordered_picture = forms.CharField()

the view (haven't sorted all the cleared_data bits and stuff yet):

def place_order(request):
    result = dict()
    if request.method == 'POST':
        try:
            form = order_form(request.POST)
            if not picture.objects.filter(id=form.ordered_picture).exists():
                result['status'] = 'error'
                result['message'] = 'Ordered picture does not exist'
            new_order = order()
            new_order.name = form.data.name
            new_order.phone = form.data.phone
            new_order.email = form.data.email
            new_order.address = form.data.address
            new_order.ordered_picture = int(form.data.ordered_picture)
            new_order.save()
            result['status'] = 'ok'
            result['message'] = new_order.id
        except Exception, e:
            result['status'] = 'error'
            result['message'] = e.message
    else:
        result['status'] = 'error'
        result['message'] = 'Only HTTP POST Method is supported by this service'
    return HttpResponse(content=simplejson.dumps(result))

Here is a copy of POST data that goes in:

csrfmiddlewaretoken:77d1ba277ef43838e670f598c2a128b6
name:qwe
phone:qwe
email:qwe
address:qwe
ordered_picture:1

and I get the following:

{"status": "error", "message": "'order_form' object has no attribute 'ordered_picture'"}

this "object has no attribute" thing is popping out at Exception right after form = order_form(request.POST) is attempted. I am about to break my head - why is this telling me the form does not have this attribute when it does in reality?

Upvotes: 3

Views: 9933

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799520

Time to sort out all the "cleaned_data bits", since that's how you access the field values.

        if not picture.objects.filter(id=form.cleaned_data['ordered_picture']).exists():

Upvotes: 4

Related Questions