Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
html, python, a simple form
#1
hello guys, i am trying to make a html form which runs a python script when submitted. I do not know where i am making a mistake and have been stuck for 3 days now! Please help.

I am using XAMPP server on my own computer.
The python code looks like this:
#!C:\Users\X\AppData\Local\Programs\Python\Python38-32\python.exe
import cgi, cgitb, datetime
cgitb.enable()

form = cgi.FieldStorage()

def get_details(key):
    if form.getvalue(key):
        return form.getvalue(key)
    else:
        print('returrning NA')
        return 'NA'

def base__builder(number):
    return get_details('element' + str(number))
    # return 'working'


def get_age(a,b, c):
    dob = datetime.date(day=a, month=b, year=c)
    age = (datetime.datetime.now().date() - dob)
    if age < datetime.timedelta(days=365):
        age = str(age/30) + ' months'
    else:
        age = str(age/365) + ' years'
    return age

def get_choice(a, b):
    for i in a:
        if base__builder(i) == 1:
            return b[i]
        else:
            continue

# 1 1 Name 
name = base__builder(1)
# 2 2 CR number
crNum = base__builder(2)
# 3,4,5 DOB 3-1 Date, 3-2 Month, 3-3 Year
age = get_age(int(base__builder('3_1')), int(base__builder('3_2')), int(base__builder('3_3')))
# age = get_age(1,2,2020)
# 6 4 Residence
residence = base__builder(6)
# 7 5 Informant
informant = base__builder(7)
# 8,9,10 6-1 Male, 6-2 Female, 6-3 Other
sex = get_choice(['6_1', '6_2', '6_3'], ['boy', 'girl', 'child with ambiguous gender'])
# 11, 12 7-1 Reliable, 7-2 Unreliable
reliability = get_choice(['7_1', '7_2'], ['reliable', 'unreliable'])


print ("Content-type: text/html")
print ("")
print ("<html><head>")
print ("")
print ("</head><body>")
print   ('%s is a {1} old {2}, a resident of {3}. The informant is the {4} and the history is {5}'.format(name, age, sex, residence, informant, reliability))
print ("</body></html>")
This is driving me nuts! The debugger is not throwing any error. Where am i going wrong..?

PS: the browser just displays this code when the form is submitted.
Reply
#2
You should not be using CGI/ XAMPP in Python anymore.
Python Wrote:cgi is going to be deprecated in Python 3.8 and removed in 3.10
Here some post you can look at one, two, three
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,695 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  pyscript index error while calling input from html form pyscript_dude 2 938 May-21-2023, 08:17 AM
Last Post: snippsat
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 877 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  Start Putty into Python Form Schlazen 5 5,331 Dec-13-2022, 06:28 AM
Last Post: divya130
  simple html page with update data korenron 3 2,585 Nov-15-2021, 09:31 AM
Last Post: jamesaarr
  Error creating database with python and form? shams 3 2,327 Aug-02-2021, 02:00 PM
Last Post: deanhystad
  Error using mariadb select query with form in python? shams 2 1,955 Jul-29-2021, 12:30 PM
Last Post: shams
  reading html and edit chekcbox to html jacklee26 5 3,020 Jul-01-2021, 10:31 AM
Last Post: snippsat
  Trying to send input from html form to database SQLite3 RonnyGiezen 3 60,869 Dec-21-2019, 02:09 PM
Last Post: ibreeden
  How to get first 5 images form the document using Python BeautifulSoup sarath_unrelax 0 1,615 Dec-19-2019, 07:13 AM
Last Post: sarath_unrelax

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020