Python Forum
html, python, a simple form - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: html, python, a simple form (/thread-28908.html)



html, python, a simple form - test - Aug-09-2020

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.


RE: html, python, a simple form - snippsat - Aug-09-2020

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