将Python变量传递到HTML

时间:2015-12-16 14:59:04

标签: python html css

#!/usr/bin/env python3
import cgi
import sqlite3
import os


path = '/var/www/html/mahesh1/trishul.db'
con = sqlite3.connect(path)
cur = con.cursor()

form = cgi.FieldStorage()
print("Content-Type:text/html\n\n")
cur.execute("SELECT * from staff")
p = cur.fetchall()
global NAME
global EMAIL

def ins():
    global to
    print(to)
    cur.execute("UPDATE staff SET password=? WHERE email_id=?", (rnd,to))
    con.commit()

def rand():
    global rnd
    rnd = ''.join(random.choice('0123456789ABCDEF') for i in range(random.choice([6,7,8,9,10])))

def email(to):
    fromaddr = "dummy.trishul@gmail.com"
    fpwd = "bilbobaggins"       
    body = 'Dear Student, your password has been set to '+rnd+'. Please Login with this password.'
    subj = "Password Issue"       
    print(to)
    yag = yagmail.SMTP(fromaddr, fpwd).send(to, subj, body)

for i in p:
    NAME = i[1]
    EMAIL = i[2]
    html1 = '''
    <html>
    <head>
    <style>
    table {
        border-collapse: collapse;
        width: 1368;
    }

    td {
        text-align:center;
        padding: 8px;
    }

    tr:nth-child(even){background-color: #f2f2f2}

    th {
        text-align:center;
        padding: 8px;
        background-color: #4CAF50;
        color: white;
    }
    div{
    padding-right:0px;
    }
    div{
    color: #FF0000
    }
    </style>
    </head>
    <body>
    <form>
    <table>
      <tr>
        <td>%s</td>
        <td>%s</td>
        <td><input type='submit' name='%s' value='APPROVE'></td>
      </tr>
     </table>
     </form>
     </body>
     </html>'''%(NAME,EMAIL,EMAIL)
    if NAME in form:
        rand()
        email(EMAIL)
        ins()
    print(html1)

未调用函数。我在数据库中通过路径显示HTML页面上的名称,电子邮件和批准按钮。当我在HTML页面上单击特定电子邮件ID和名称的批准按钮时,它应该将邮件发送到相应的电子邮件ID。

1 个答案:

答案 0 :(得分:0)

在您的表单中,您没有指定任何操作。 <form action='myscript.py'>将请求发送到该脚本,服务器将执行脚本,为其提供表单数据作为参数。 (复制自我上面的评论)