当使用Python脚本服务器sid处理时,提交的表单没有从HTML文本输入中获取重新启用的值

2024-05-17 19:53:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表单,让用户有机会输入一个项目名称,然后单击一个按钮“设置”它。然后运行一个JavaScript来禁用输入框和按钮,并启用一些需要为项目设置的其他字段。当单击提交时,我运行另一个JavaScript来重新启用禁用的项目名称HTML文本输入框。你知道吗

当项目被提交时,由于某种原因,项目名称不会被提交,而表单中的所有内容都会被提交。我尝试在表单头标签中使用onSubmit=,在提交按钮上也使用onClick。你知道吗

我正在使用Python处理服务器端。你知道为什么我没有在表单提交中得到projectName变量吗?你知道吗

我从python脚本中得到的错误是:

/var/www/ARCHlang/cgi-bin/pythonTest.cgi in ()    
32 myN = 0    
33 myC = 0
34 projectPath = "../prPages/userFiles/" + projectName + "/"    
35 mainTclPath = projectPath + "pr.tcl"    
36 systemCheckpointToOpen = "" projectPath undefined, projectName = None
<type 'exceptions.TypeError'>: cannot concatenate 'str' and 'NoneType'
objects
args = ("cannot concatenate 'str' and 'NoneType' objects",)
    message = "cannot concatenate 'str' and 'NoneType' objects"

代码如下:

HTML格式:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script language="Javascript">
var globalCounter = 0;

function SelectMoveRows(SS1,SS2)
{
    var SelID='';
    var SelText='';
    // Move rows from SS1 to SS2 from bottom to top
    for (i=SS1.options.length - 1; i>=0; i--)
    {
        if (SS1.options[i].selected == true)
        {
            SelID=SS1.options[i].value;
            SelText=SS1.options[i].text;
            var newRow = new Option(SelText,SelID);
            SS2.options[SS2.length]=newRow;
            SS1.options[i]=null;
        }
    }
    SelectSort(SS2);
}

function SelectSort(SelList)
{
    var ID='';
    var Text='';
    for (x=0; x < SelList.length - 1; x++)
    {
        for (y=x + 1; y < SelList.length; y++)
        {
            if (SelList[x].text > SelList[y].text)
            {
                // Swap rows
                ID=SelList[x].value;
                Text=SelList[x].text;
                SelList[x].value=SelList[y].value;
                SelList[x].text=SelList[y].text;
                SelList[y].value=ID;
                SelList[y].text=Text;
            }
        }
    }
}



function RemoveUserAcc(){


}


function RunOnSubmit(){
    for (var i = 0; i < selectBox.options.length; i++) {
        document.projectBuild.userSelections.options[i].selected = true; 
    } 

    document.projectBuild.projectName.disabled = false;

}

function checkUserInput(theInputBox){

    if(theInputBox.value == ""){
        return false;
    }

    var re = /^[a-zA-Z0-9\-_]{0,40}$/;

    if(!re.test(theInputBox.value)){
        alert("Error: Alphanumeric keys only. 40 character max. No spaces.");
        theInputBox.focus(); 
        return false;
    }
 // validation was successful 
    return true;
}

function EnableElements(){ 

    //check user input is okay to be a folder name
    if(checkUserInput(document.projectBuild.projectName)){
    //disable project name element and set project name button
    document.projectBuild.projectName.disabled = true;
    document.projectBuild.setProjectName.disabled = true;

    //enable all other form elements
    document.projectBuild.platform.disabled = false;
    document.projectBuild.numOfHemps.disabled = false;
    document.projectBuild.selectList.disabled = false;
    document.projectBuild.add.disabled = false;
    document.projectBuild.remove.disabled = false;
    document.projectBuild.userSelections.disabled = false;
    document.projectBuild.userCreatedAcc.disabled = false;
    document.projectBuild.userAccFilenameInput.disabled = false;
    document.projectBuild.userAccAdd.disabled = false;
    document.projectBuild.userAccRemove.disabled = false;
    document.projectBuild.submit.disabled = false;
    }
}

function AddUserAcc(){

    var accFileName = document.projectBuild.userAccFilenameInput.value;
    var newRow = new Option(accFileName,accFileName);
    document.projectBuild.userCreatedAcc.options[document.projectBuild.userCreatedAcc.length] = newRow;

}


</script>

<html>

<head>
<title>CSDL MPSoPC</title>
<!--
<![if !IE]>  <link href="../styles/screen.css" rel="stylesheet" type="text/css" media="screen" /> <![endif]>
-->

<![if !IE]>  <link href="cssTest.css" rel="stylesheet" type="text/css" media="screen" /> <![endif]>

</head>

<body>


    <div id="header">
        <h1><img src = "../images/cloud.png"/></h1>
    </div>

    <div id="menu">
        <center>
        <ul>
            <a href="../index.html"><li>Home</li></a>
            <a href="../prebuilt.html"><li></li></a>
            <a href="../buildyourown.html"><li></li></a>
            <a href="../compile.html"><li></li></a>
            <a href="hempBuildv1.html"><li></li></a>
            <a href="http://hthreads.csce.uark.edu/wiki/Main_Page"><li></li></a>

        </ul>
        <img src="../images/uofa.png" />
        </center>

    </div>





<!-- MAIN PAGE CONTENT/ THIS IS THE FORM -->
    <div id="content">


        <form name="projectBuild" 
            onSubmit="RunOnSubmit()" 
            action="../cgi-bin/pythonTest.cgi" 
            enctype="multipart/form-data" 
            method="POST">

        <table border="1" cellpadding="0" cellspacing="0" align="center">


            <tr> 
                <td colspan=3>
                    <b>Build Your Own HEMPS System</b>
                </td> 
            </tr>

            <tr>
                <td colspan="3">
                    <p align = "justify">You can create your own system 
                </td>   
            </tr>
<!--            </table>

            <table border="1" cellpadding="0" cellspacing="0" align="center">
-->     
            <!--<tr> 
                <td align="left">
                    <b>Global Parameters</b>

                </td>
            </tr>

            <tr>
                <td>
                    </br>
                </td>
            </tr>-->
            <tr>
                <td colspan="3"><hr></td>
            </tr>
            <tr>
                <td colspan="3">
                    <b>First set project name to enable other system options.</b>
                </td>
            </tr>
            <tr>
                <td colspan="3"><hr></td>
            </tr>

            <tr>
                <td>
                    Project Name:
                </td>
                <td colspan=2>
                    <input type="text" size="30" name="projectName" >
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <input type="button" name="setProjectName" value="Set Project Name" onClick="EnableElements(document.projectBuild.projectName)" >
                </td>
            </tr>
            <tr>
                <td colspan="3"><hr></td>
            </tr>
            <tr>
                <td>Board: </td>
                <td colspan=2>
                    <select name = "platform" size = "1" disabled="disabled">
                        <option selected value="vc707"> VC707 </option>
                    </select>
                </td>
            </tr>

            <tr>    
                <td>System: </td>
                <td colspan=2>
                    <select name = "numOfHemps" size = "1" disabled="disabled">
                        <option select value="2">2 Node</option>
                        <option value="3">3 Node </option>
                        <option value="4">4 Node </option>
                        <option value="5">5 Node </option>
                        <option value="6">6 Node </option>  
                        <option value="7">7 Node </option>                  
                        <option value="8">8 Node </option>
                        <!--<option value="9">9 Node</option>-->
                    </select>
               </td>
            </tr>

            <tr>
                <td>
                    </br>
                </td>
            </tr>


            <tr>
                <td style="vertical-align:top" colspan=3>
                    Select the accelerators for your project:
                </td>
            </tr>
            <tr>
                <td>
                    <b><u>Pre-Built Accelerators</u></b>
                </td>
                <td>
                </td>
                <td>
                    <b><u>Added Pre-Built Accelerators</u></b>
                </td>
            </tr>
            <tr>

                <td align="center">

                    <SELECT NAME="selectList" size="5" style="width: 100%" MULTIPLE disabled="disabled">
                        <OPTION VALUE="crc">CRC</option>
                        <OPTION VALUE="vector">Vector</option>
                        <OPTION VALUE="mm">Matrix Multiply</option>
                        <OPTION VALUE="quicksort">Quick Sort</option>-->
                    </SELECT>

                </td>
                <td align="center" >

                        <input type="button" name="add" value="Add       >>" onClick="SelectMoveRows(document.projectBuild.selectList,document.projectBuild.userSelections)" disabled="disabled">
                            <br>
                        <input type="button" name="remove" value="<< Remove" onClick="SelectMoveRows(document.projectBuild.userSelections,document.projectBuild.selectList)" disabled="disabled">

                </td>
                <td allign="center">
                    <SELECT NAME="userSelections" size="5" style="width: 100%" MULTIPLE disabled="disabled">
                    </SELECT>
                </td>

            </tr>
            <tr>
                <td>
                    <br>
                </td>
            </tr>

            <tr>
                <td>
                    <b><u>User Added Accelerators</u></b>
                </td>
            </tr>
            <tr>
                <td>
                    <SELECT name="userCreatedAcc" size="5" style="width: 100%" MULTIPLE disabled="disabled"> <!--disabled="disabled"-->
                    <!--<OPTION VALUE="1">spaceHolder1</option>
                    <OPTION VALUE="2">spaceHolder2</option>
                    <OPTION VALUE="3">spaceHolder3</option>-->
                    </SELECT>
                </td>
            </tr>
            <tr>
                <td colspan=2>
                    Enter Filename:
                    <input type="text" size="30" name="userAccFilenameInput"  disabled="disabled">
                </td>
                <td>

                </td>
            </tr>

            <tr>
                <td>
                    <input type="button" name="userAccAdd" value="Add User Accelerator" onClick="AddUserAcc()" disabled="disabled"> <!--disabled="disabled"-->
                    <input type="button" name="userAccRemove" value="Remove User Accelerator"  onClick="EnableIfInputCorrect()" disabled="disabled">
                </td>
            </tr>


            <tr>
                <td>
                    </br></br>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td align="center"> 
                    <input type="submit" name="submit" value="Submit" onClick = "RunOnSubmit()" disabled="disabled">
                </td>
                <td>
                </td>
            </tr>

        </table>
        </form>
    </div>
</body>


</html>

Python:

#!/usr/bin/python
import cgi, os, sys, commands
import zipfile
import os.path

#
import cgitb
cgitb.enable()

import shutil

print "Content-type: text/html\n\n"
print "<html>\n"
print "<head>\n"

###################################################################################
#Import form data
###################################################################################
myForm = cgi.FieldStorage()
projectName = myForm.getvalue("projectName")
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#platform varible is only correct for vc707 board
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
platform = myForm.getvalue("platform")<----------------HERE IS WHERE IT IS ASSIGNED. 
numOfHemps = myForm.getvalue("numOfHemps")
pickedAcc = myForm.getvalue("userSelections")
leftAcc = myForm.getvalue("selectList")
userCreatedAccList = myForm.getvalue("userCreatedAcc")

accPath = "../prPages/pr/acc/"
print myForm
myN = 0
myC = 0
projectPath = "../prPages/userFiles/" + projectName + "/"     <--------HERE IS THE PROBLEM
mainTclPath = projectPath + "pr.tcl"
systemCheckpointToOpen = ""
userFilePath = "../prPages/userFiles/"


###########################################################################
#If there are already N, 20 as of now, user projects clear out user projects
#and start with none.
############################################################################
num_files = len(os.listdir(userFilePath))
if num_files > 18:
    shutil.rmtree(userFilePath, True)
    if not os.path.exists(userFilePath):
        os.mkdir(userFilePath)

###################################################################################
#Create Project Directory and sub-directories
###################################################################################
#print "Project Name: " + projectName + "<br>"

if not os.path.exists(projectPath):
    os.mkdir(projectPath)
    os.mkdir(projectPath+"acc/")
    os.mkdir(projectPath+"system/")
else:
    shutil.rmtree(projectPath)
    os.remove("../prPages/userFiles/" + projectName + ".zip")
    os.mkdir(projectPath)
    os.mkdir(projectPath+"acc/")
    os.mkdir(projectPath+"system/")



###################################################################################
#Copy all acc files over that are needed
###################################################################################
#Run through all select Acc and copy them to our project
if type(pickedAcc) is str:
    #One acc selected
    shutil.copyfile(accPath+pickedAcc+".dcp", projectPath+"acc/"+pickedAcc+".dcp")
else:
    #Multiple acc selected
    for x in range(0, len(pickedAcc)):
        shutil.copyfile(accPath+pickedAcc[x]+".dcp", projectPath+"acc/"+pickedAcc[x]+".dcp")


###################################################################################
#numOfHemps variable determines N and C value as well 
#as the static sytem .dcp file to copy over
###################################################################################
if numOfHemps == "2":
    shutil.copyfile("../prPages/pr/system/N1C2.dcp", projectPath+"system/N1C2.dcp" )
    myN = 1
    myC = 2
if numOfHemps == "3":
    shutil.copyfile("../prPages/pr/system/N1C3.dcp", projectPath+"system/N1C3.dcp" )
    myN = 1
    myC = 3
elif numOfHemps == "4":
    shutil.copyfile("../prPages/pr/system/N2C2.dcp", projectPath+"system/N2C2.dcp" )
    myN = 2
    myC = 2
if numOfHemps == "5":
    shutil.copyfile("../prPages/pr/system/N1C5.dcp", projectPath+"system/N1C5.dcp" )
    myN = 1
    myC = 5
elif numOfHemps == "6":
    shutil.copyfile("../prPages/pr/system/N2C3.dcp", projectPath+"system/N2C3.dcp" )
    myN = 2
    myC = 3
if numOfHemps == "7":
    shutil.copyfile("../prPages/pr/system/N7C1.dcp", projectPath+"system/N7C1.dcp" )
    myN = 7
    myC = 1
elif numOfHemps == "8":
    shutil.copyfile("../prPages/pr/system/N2C4.dcp", projectPath+"system/N2C4.dcp" )
    myN = 2
    myC = 4



###################################################################################
#platform variable determines which fpga we are using and
#also which pr region file to use
###################################################################################
if platform == "zedBoard":
    shutil.copyfile("../prPages/pr/zedboard.tcl", projectPath+"zedboard.tcl")
elif platform == "microZed":
    shutil.copyfile("../prPages/pr/microZed.tcl", projectPath+"microZed.tcl")
elif platform == "ac701":
    shutil.copyfile("../prPages/pr/ac701.tcl", projectPath+"ac701.tcl")
elif platform == "zc702":
    shutil.copyfile("../prPages/pr/zc702.tcl", projectPath+"zc702.tcl")
elif platform == "vc707":
    shutil.copyfile("../prPages/pr/vc707.tcl", projectPath+"vc707.tcl")


###################################################################################
#Open each pr.tcl file, one to read from and one to write to.
###################################################################################
inputTcl = open("../prPages/pr/pr.tcl", "r")
outputTcl = open(mainTclPath,"w")


###################################################################################
#Open and edit pr.tcl as needed
#Set N, C, system checkpoint to open, fpga/platform, acc list
###################################################################################
if True:
    for line in inputTcl:
        if line == "set N\n":
            line = line.rstrip('\n')
            outputTcl.write(line + " " + str(myN) +"\n")
        elif line == "set C\n":
            line = line.rstrip('\n')
            outputTcl.write(line +  " " + str(myC) +"\n")
        elif line == "open_checkpoint\n":
            line = line.rstrip('\n')
            outputTcl.write(line + " system/N" + str(myN) +"C" + str(myC) + ".dcp\n")
        elif line == "set fpga\n":
            line = line.rstrip('\n')
            outputTcl.write(line + " " + platform + "\n")
        elif line == "set list_acc\n":
            line = line.rstrip('\n')
#accList is a combination of the pre-made acc and the user entered filenames 
            accList = ""
            if type(pickedAcc) is str:
                #One acc selected
                accList = pickedAcc + " "
            else:
                #Multiple acc selected
                for x in range(0, len(pickedAcc)):
                    accList = accList + pickedAcc[x] + " "





            outputTcl.write(line + " { " + accList + "}\n")
        else:
            outputTcl.write(line)



inputTcl.close()
outputTcl.close()

###################################################################################
#Copy shell script over to user project
###################################################################################
shutil.copyfile("../prPages/pr/synthProject.sh", projectPath+"synthProject.sh" )


###################################################################################
#Create zip file of users project
###################################################################################
userZipFile = zipfile.ZipFile("../prPages/userFiles/" + projectName + ".zip", "w")
for root, dirs, files in os.walk("../prPages/userFiles/" + projectName + "/"):
    for file in files:
        userZipFile.write(os.path.join(root, file), root[21:] + "/" + file)
userZipFile.close()


###################################################################################
#Setup Html page
###################################################################################
print """

<title>CSDL MPSoPC</title>
<![if !IE]>  <link href="../styles/screen.css" rel="stylesheet" type="text/css" media="screen" /> <![endif]>
</head>
<body>
<div id="header">
        <h1><img src = "../images/cloud.png"/></h1>
    </div>

    <div id="menu">
        <center>

        <img src="../images/u.png" />
        </center>

    </div>

    <div id="content">
        <b> How to use:</b></br>

        <br><br><br>
        Here are your files.<br>
"""

print       '<a href="' + 'http://noneyobezzwax/ARCHlang/prPages/userFiles/' + projectName + '.zip" >Design Files</a>'

print """
    </div>



</body>
</html>"""

Tags: ifvaluelineprdocumentsystemtrtd