apardes
apardes

Reputation: 4380

Django try..except ValueError raised with no error

I am trying to allow users to submit data optionally by including try:...except ValueError: to catch missing data, however the except is triggered even when all input values are included.

def sByInfo(request):
    print "\n \n NEW CALL"

    ranked = Ranked.objects.all()
    unranked = Unranked.objects.all()

    ranked_matches = [] # List for ranked institution matches
    unranked_matches = [] # List for unranked institution matches

    try:
        gpa = request.GET['gpa']
        gpa = float(gpa)
        print gpa
        pcat = request.GET['pcat']
        pcat = int(pcat)
        print pcat

        try:
            city = request.GET['city']
            state = request.GET['state']
            print "{}, {}".format(city, state)

            position = getPos(city, state)
            lati = Decimal(position['lat'])
            loni = Decimal(position['lon'])

            print "\n RANKED"
            for x in ranked:
                print x.name
                average_gpa = (x.gpa_expected + x.gpa_overall) / 2
                print average_gpa
                if gpa >= average_gpa:
                    print "GPA good"
                    if ranked_matches.index(x):
                        print "School already added"
                    else:
                        ranked_matches.append(x)
                        print "School added"
                else:
                    print "GPA too low"

                if pcat >= x.min_pcat:
                    if ranked_matches.index(x):
                        print "School already added"
                    else:
                        ranked_matches.append(x)
                else:
                    print "PCAT too low"

                lat = Decimal(x.lat)
                lon = Decimal(x.lon)
                difference = posDifference(lati, loni, lat, lon)
                print "Distance is {} miles".format(difference)

                if difference <= 150:
                    if ranked_matches.index(x):
                        print "School already added"
                    else:
                        ranked_matches.append(x)
                else:
                    print "School out of range"

            print "\n UNRANKED"
            for y in unranked:
                print y.name
                average_gpa = (y.gpa_expected + y.gpa_overall) / 2
                if gpa >= average_gpa:
                    if unranked_matches.index(y):
                        print "School already added"
                    else:
                        unranked_matches.append(y)
                else:
                    print "GPA too low"

                if pcat >= y.min_pcat:
                    if unranked_matches.index(y):
                        print "School already added"
                    else:
                        unranked_matches.append(y)
                else:
                    print "PCAT too low"

                lat = Decimal(y.lat)
                lon = Decimal(y.lon)
                difference = posDifference(lati, loni, lat, lon)
                print "Distance is {} miles".format(difference)

                if difference <= 150:
                    if unranked_matches.index(y):
                        print "School already added"
                    else:
                        unranked_matches.append(y)
                else:
                    print "School out of range"



        except ValueError: ## City or State was not submitted
            print "City or state missing"

            try:
                state = request.GET['state']
            except ValueError:
                print "City and state missing"

    except ValueError:
        return render('You must enter both GPA & PCAT scores')


    return render_to_response('results.html', {'ranked_matches' : ranked_matches, 'unranked_matches' : unranked_matches}, context_instance = RequestContext(request))

The first for loop gets through a single iteration with no noticeable failures and then the first nested exception is raised returning the "City or state missing" message. I am confused as to why this exception is raised as all values were submitted.

I believe the issue must be somewhere in this if statement

if ranked_matches.index(x):
    print "School already added"
else:
    ranked_matches.append(x)
    print "School added"

All help is appreciated, thanks.

Upvotes: 0

Views: 2682

Answers (1)

dm03514
dm03514

Reputation: 55962

I dont think ValueError is robust enough,

if the data is incorrect data type

int('test')

it raises a ValueError, so its not a sufficient way to check for the presence of fields.

A more robust way is to use djangos built in Forms. It will allow you to specify data types and required fields, and django will take care of the validation for you.

Upvotes: 1

Related Questions