473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1.  try:
  2.         price2 = (float(orderformdata['product_qty2'])) * int(orderformdata['product_price2']);         
  3.         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)
  4.     except:
  5.         if price2 is null
  6.             then
  7.              the result is 'EXIT', termg
  8.         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
Mar 4 '08 #1
8 1824
bvdet
2,851 Expert Mod 2GB
Judging by the error message, the value returned by orderformdata['product_qty2'] is an empty string. Check the dictionary data.
Mar 4 '08 #2
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!
Mar 5 '08 #3
bvdet
2,851 Expert Mod 2GB
One way do do that:
Expand|Select|Wrap|Line Numbers
  1. if orderformdata['product_qty2']:
  2.     ...process normally...
  3. else:
  4.     ...do something else...
Mar 5 '08 #4
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
Mar 5 '08 #5
jlm699
314 100+
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:
Expand|Select|Wrap|Line Numbers
  1. >>> d = {}
  2. >>> d['a'] = 'test'
  3. >>> d['b'] = 5
  4. >>> d['c'] = ''
  5. >>> bool(d['a'])
  6. True
  7. >>> bool(d['b'])
  8. True
  9. >>> bool(d['c'])
  10. False
  11. >>> 
  12.  
Mar 5 '08 #6
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
Mar 5 '08 #7
jlm699
314 100+
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:

Expand|Select|Wrap|Line Numbers
  1. print 'Here we go'
  2. a = 7
  3. b = 5
  4. print 'Entering if structure'
  5.  
  6. if a == b:
  7.     print 'Equal'
  8. elif a <= b:
  9.     print 'A is less than or equal to B'
  10. elif a >= b and a != 8 and b > 1:
  11.     print 'A and B satisfied all conditions'
  12. else:
  13.     print 'No conditions were met'
  14.  
  15. # Regular code continues here...
  16. print 'Back from the if structure and into normal processing'
  17.  
Mar 6 '08 #8
Python is all about formatting...

A complete if structure:

Expand|Select|Wrap|Line Numbers
  1. print 'Here we go'
  2. a = 7
  3. b = 5
  4. print 'Entering if structure'
  5.  
  6. if a == b:
  7.     print 'Equal'
  8. elif a <= b:
  9.     print 'A is less than or equal to B'
  10. elif a >= b and a != 8 and b > 1:
  11.     print 'A and B satisfied all conditions'
  12. else:
  13.     print 'No conditions were met'
  14.  
  15. # Regular code continues here...
  16. print 'Back from the if structure and into normal processing'
  17.  
Aaaaah ok! That makes sense... Sorry I didnt respond sooner been very busy!

And btw... That helped immensly! Thanks!
Mar 11 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
6
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
3
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
9
by: MrDeej | last post by:
Hello guys! We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.