Error Handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Modern Merlin
    New Member
    • Nov 2007
    • 12

    Error Handling

    Ok so I am COMPLETELY new to PYTHON and Django all together. I am currently working on a form for a django site. In my views.py it sends an email filled with the data from that form. Basically, its an order form that 4 cloumns (Product, Color, Qty, Price/ea) with 10 rows someone can fill out.

    What I am trying to do in the email is have it print those for columns as many times as there is a product. So product1 is required and product2-10 is not. Also I should add that I have defined price1-10 for the ability to let the recipient know how much ammount is (it takes the price/ea and multiplies it by the quantity.) Hence the price2 you will see below.

    If a customer fills in product1 and product2 for two seperate products, but not any other products I want views.py to quit building the email after product2.

    Currently here is what I have to try and handle this:
    Code:
     try:
            price2 = (float(orderformdata['product_qty2'])) * int(orderformdata['product_price2']);         
            body+= "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % (orderformdata['product_name2'],orderformdata['product_color2'],orderformdata['product_qty2'],orderformdata['product_price2'],price2)
        except:
            if price2 is null
                then
                 the result is 'EXIT', termg
            pass
    And here is the error I am getting...

    ValueError at /order-form/
    empty string for float()
    Request Method: POST
    Exception Type: ValueError
    Exception Value: empty string for float()


    Any help someone could be with this would be wonderful! Thanks!

    MM
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Judging by the error message, the value returned by orderformdata['product_qty2'] is an empty string. Check the dictionary data.

    Comment

    • Modern Merlin
      New Member
      • Nov 2007
      • 12

      #3
      It is empty, what I need to know is what is the correct way to tell it to quit processing that line and:

      a) Either quit processing and send the email or
      b) Quit processing that line and go to the next, which would have the same code and so on...

      Thanks!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        One way do do that:[code=Python]
        if orderformdata['product_qty2']:
        ...process normally...
        else:
        ...do something else...[/code]

        Comment

        • Modern Merlin
          New Member
          • Nov 2007
          • 12

          #5
          Doesnt that mean that if it is product2 then process normally and if it isnt then do something else?

          If thats what it means thats not what I need it to do. I need it to go through product fields 1-10 If 1-3 have something in them but do not have anything in field 4 then I need it to stop after 3 and email it. If the form had products in field 1-7 then I need it to stop after 7.

          Im sorry if Im not making much sense, like I said before, I really am green at this and havent programmed for about a year LOL

          Comment

          • jlm699
            Contributor
            • Jul 2007
            • 314

            #6
            Originally posted by Modern Merlin
            Doesnt that mean that if it is product2 then process normally and if it isnt then do something else?
            No, that says that if there is a value in orderformdata['product_qty2'] then do something... else do something else...

            Similarly, the following demonstrates:
            [CODE=python]
            >>> d = {}
            >>> d['a'] = 'test'
            >>> d['b'] = 5
            >>> d['c'] = ''
            >>> bool(d['a'])
            True
            >>> bool(d['b'])
            True
            >>> bool(d['c'])
            False
            >>>
            [/CODE]

            Comment

            • Modern Merlin
              New Member
              • Nov 2007
              • 12

              #7
              Aaaah ok I re-read it and realized my mistake.. So is Term not the correct way to leave that or would it be

              endif

              Comment

              • jlm699
                Contributor
                • Jul 2007
                • 314

                #8
                Originally posted by Modern Merlin
                Aaaah ok I re-read it and realized my mistake.. So is Term not the correct way to leave that or would it be

                endif
                Python is all about formatting...

                A complete if structure:

                [CODE=python]
                print 'Here we go'
                a = 7
                b = 5
                print 'Entering if structure'

                if a == b:
                print 'Equal'
                elif a <= b:
                print 'A is less than or equal to B'
                elif a >= b and a != 8 and b > 1:
                print 'A and B satisfied all conditions'
                else:
                print 'No conditions were met'

                # Regular code continues here...
                print 'Back from the if structure and into normal processing'
                [/CODE]

                Comment

                • Modern Merlin
                  New Member
                  • Nov 2007
                  • 12

                  #9
                  Originally posted by jlm699
                  Python is all about formatting...

                  A complete if structure:

                  [CODE=python]
                  print 'Here we go'
                  a = 7
                  b = 5
                  print 'Entering if structure'

                  if a == b:
                  print 'Equal'
                  elif a <= b:
                  print 'A is less than or equal to B'
                  elif a >= b and a != 8 and b > 1:
                  print 'A and B satisfied all conditions'
                  else:
                  print 'No conditions were met'

                  # Regular code continues here...
                  print 'Back from the if structure and into normal processing'
                  [/CODE]
                  Aaaaah ok! That makes sense... Sorry I didnt respond sooner been very busy!

                  And btw... That helped immensly! Thanks!

                  Comment

                  Working...