Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

jang walia
jang walia
7,717 Points

getting quantity, price and total price

(THIS IS PYTHON FILE DIRECTED THROUGH FORM ACTION) I AM UNABLE TO TRANSFER VALUES OF "quantity" and "price" into the input text and in the end get the total amount by multiplying both "quantity" and "price" to get" total_pay" input value...PLEASE HELP ANYONE!!

#!C:\Users\hello\Desktop\PYTHON\python.exe
import cgi,cgitb
import os
form=cgi.FieldStorage()
i=form.getvalue("image")
p=form.getvalue("productID")
q=form.getvalue("quantity")
pr=form.getvalue("price")
sz=form.getvalue("select_size")
print("Content-type:text/html")
print("")
print('<html>'
      '<head>'
      '<script>'

      '</script>'
      '</head>'
      '<body>'
      '<h1>the product id is :', p ,'</h1>'
      '<p>the size choosen is :',sz,'</p>'
      '<p>the quantity is </p>'
      '<input type ="text" id ="qnts">'
      '<p>the price of this product is :</p>'
      '<input type ="text" id ="cost_product">'
      '<p>Total payable amaount</p>'
      '<input type ="text" id ="total_pay" '
      '</body>'
      '</html>')

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Glad to see you posting in the forums! That is a great step toward becoming a developer.

I highly recommend you take the Treehouse course on Flask. It has a lot of great information to get you started in web programming. Kenneth Love is one of the best Flask teachers you can find, and you may find a Flask app slightly easier to get running consistently than straight CGI. And, the cool thing about Flask is it can be deployed as a CGI script as well as in other scenarios. Treehouse workspaces have everything you need to get up and running.

That being said...

I can see you are doing some old-school Python CGI programming-- let's build on that...

There are multiple problems you will need to address before it will work:

  1. you are missing the <form method="GET"> tag at the beginning of the form part of your HTML
  2. your input elements need a name parameter which matches its getvalue ("image", productID", "quantity", "price", "select_size")
  3. you should include an <input type="submit"> button.
  4. you need a closing </form> tag.
  5. you might want to look carefully at the CGI programming link below to get the output string to format the way you intend.

Here is a basic treatment of Python CGI programming:

https://www.tutorialspoint.com/python/python_cgi_programming.htm