我正在尝试使用Python将列表传递到HTML表单。我是一个菜鸟,我不确定我在做什么,所以任何建议都会受到赞赏。
我要做的是填写所有空白文本框,然后单击单选按钮并使用列表下拉列表/菜单。此列表将是表单的默认值。
form = cgi.FieldStorage()
latitude = form.getvalue('latitude', '0')
if config_settings.settings[0]:
latitude = config_settings.settings[0]
我一直试图用CGI模块做到这一点,但我没有做到这一点。我应该使用机械或硒,还是可以用CGI和FieldStorage完成。任何建议都将不胜感激。
#!/usr/bin/python
import config_settings
import cgi
import cgitb
# A path to error logs
cgitb.enable(display=0,logdir="/var/www/cgi-bin/error-logs")
print("Content-Type: text/html\n\n")
print("")
print('''<html>
<head>
<title>EM2010 Sound Level Monitor - Setup</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="EM2010 User Interface">
<meta name="author" content="Sonitus Systems">
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<div class="logo"> <a href="/index.html"> <img src="../images/sonitus_logo_halo.png" style="height:32px;" /> </a> </div>
<a class="brand" href="/index.html">EM2010 Sound Level Monitor</a>
<div class="nav-collapse collapse">
<p class="navbar-text pull-right">
<a href="./set_time.cgi" class="navbar-link"> <span id="showdate"> </span><span id="showtime"> </span> </a>
</p>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span10 offset1">
<!--This is the line you need to look at mark-->
<form class="well form-inline" method="post" action="/cgi-bin/process_setup.cgi">
<!-- Location -->
<i class="icon-location-arrow icon-large"> <span class="setting"> Location<span></span></i><br><br>
Latitude: <input type="text" name="latitude" class="input-small" value="lat">°
<select name="latHemi">
<option selected="selected">N</option>
<option>S</option></select>
<option>N</option>
<option selected="selected">S</option></select>
Longitude: <input type="text" name="longitude" class="input-small" value="$long">°
<select name="longHemi">
<option selected="selected">E</option>
<option>W</option></select>
<option>E</option>
<option selected="selected">W</option></select>
<hr/>
<!-- Mic Sensitivity -->
<i class="icon-microphone icon-large"> <span class="setting"> Microphone<span></span></i><br><br>
Sensitivity: <input type="text" name="sensitivity" class="input-small" value="$micSensitivity"> dB
<hr/>
<!-- Measurement Settings -->
<i class="icon-edit icon-large"> <span class="setting"> Measurement Settings<span></span></i><br><br>
<h5>Weighting:</h5>
<label class="checkbox inline control-label"><input name="aWeight" value="aWeight" checked="checked" readonly="readonly" disabled="disabled" type="checkbox">
<span> A-Weight </span></label>
<label class="checkbox inline control-label"><input name="cWeight" value="cWeight" checked="checked" type="checkbox">
<span> C-Weight</span></label>
<br>
<br>
<h5>Optional Levels (L<sub>EQ</sub> is always recorded):</h5>
<label class="checkbox inline control-label"><input name="L95" value="L95" checked="checked" type="checkbox">
<!--<label class="checkbox inline control-label"><input name="L95" value="L95" type="checkbox">-->
<span> L95 </span></label>
<label class="checkbox inline control-label"><input name="L90" value="L90" checked="checked" type="checkbox">
<!--<label class="checkbox inline control-label"><input name="L90" value="L90" type="checkbox">-->
<span> L90 </span></label>
<label class="checkbox inline control-label"><input name="L50" value="L50" checked="checked" type="checkbox">
<!--<label class="checkbox inline control-label"><input name="L50" value="L50" type="checkbox">-->
<span> L50 </span></label>
<label class="checkbox inline control-label"><input name="L10" value="L10" checked="checked" type="checkbox">
<!--<label class="checkbox inline control-label"><input name="L10" value="L10" type="checkbox">-->
<span> L10 </span></label>
<label class="checkbox inline control-label"><input name="L05" value="L05" checked="checked" type="checkbox">
<!--<label class="checkbox inline control-label"><input name="L05" value="L05" type="checkbox">-->
<span> L5 </span></label>
<label class="checkbox inline control-label"><input name="fmax" value="fmax" checked="checked" type="checkbox">
<!--<label class="checkbox inline control-label"><input name="fmax" value="fmax" type="checkbox">-->
<span> L<sub>MAX</sub></span></label>
<br>
<br>
<h5>Averaging Period:</h5>
<label class="radio inline control-label"><input name="epoc" value="1min" checked="checked" type="radio">
<!--<label class="radio inline control-label"><input name="epoc" value="1min" type="radio">-->
<span> 1 minute </span></label>
<label class="radio inline control-label"><input name="epoc" value="5min" checked="checked" type="radio">
<!--<label class="radio inline control-label"><input name="epoc" value="5min" type="radio">-->
<span> 5 minutes </span></label>
<label class="radio inline control-label"><input name="epoc" value="10min" checked="checked" type="radio">
<!-- <label class="radio inline control-label"><input name="epoc" value="10min" type="radio">-->
<span> 10 minutes </span></label>
<label class="radio inline control-label"><input name="epoc" value="15min" checked="checked" type="radio">
<!--<label class="radio inline control-label"><input name="epoc" value="15min" type="radio">-->
<span> 15 minutes </span></label>
<label class="radio inline control-label"><input name="epoc" value="30min" checked="checked" type="radio">
<!--<label class="radio inline control-label"><input name="epoc" value="30min" type="radio">-->
<span> 30 minutes</span></label>"
<br>
<br>
<h5>Time Weighting (L<sub>MAX</sub>):</h5>
<label class="radio inline control-label"><input name="fastaveraging" value="fastaveraging" checked="checked" type="radio">
<span> 0.125s (Fast) </span></label>
<label class="radio inline control-label"><input name="fastaveraging" value="empty" type="radio">
<span> 1s (Slow)</span></label>
<label class="radio inline control-label"><input name="fastaveraging" value="fastaveraging" type="radio">
<span> 0.125s (Fast) </span></label>"
<label class="radio inline control-label"><input name="fastaveraging" value="empty" checked="checked" type="radio">
<span> 1s (Slow)</span></label>"
<hr/>
<!-- Reboot -->
<i class="icon-refresh icon-large"> <span class="setting"> Reboot Time<span></span></i><br><br>
<label class="radio inline control-label"><input name="bootTime" value="midnight" checked="checked" type="radio">
<!--<label class="radio inline control-label"><input name="bootTime" value="midnight" type="radio">-->
<span >00:00hrs</span></label>
<label class="radio inline control-label"><input name="bootTime" value="7am" checked="checked" type="radio">
<!--<label class="radio inline control-label"><input name="bootTime" value="7am" type="radio">-->
<span >07:00hrs</span></label>
<label class="radio inline control-label"><input name="bootTime" value="7pm" checked="checked" type="radio">
<!--<label class="radio inline control-label"><input name="bootTime" value="7pm" type="radio">-->
<span >19:00hrs</span></label>
<label class="radio inline control-label"><input name="bootTime" value="23pm" checked="checked" type="radio">
else
<!--<label class="radio inline control-label"><input name="bootTime" value="23pm" type="radio">-->
<span >23:00hrs</span></label>
<hr/>
<!-- ISP -->
<i class="icon-cloud-upload icon-large"> <span class="setting"> Remote Upload<span></span></i><br><br>
<label class="radio inline control-label"><input name="isp" value="nointernet" checked="checked" type="radio">
else
<label class="radio inline control-label"><input name="isp" value="nointernet" type="radio">
<span>Upload Off</span></label>
<label class="radio inline control-label"><input name="isp" value="vodafone" checked="checked" type="radio">
<label class="radio inline control-label"><input name="isp" value="vodafone" type="radio">
<span>Upload On</span></label>
<hr/>
Changes will not take effect until the monitor is <span class="bold">rebooted</span>.
<p class="offset0">
<br/>
<label for="submit" class="btn"><i class="icon-ok"></i> Submit Changes</label>
<input id="submit" name="Submit" value="Submit Changes" type="submit" class="hidden" />
<label for="reset" class="btn"><i class="icon-refresh"></i> Reset Form</label>
<input id="reset" name="Reset" value="Reset Form" type="reset" class="hidden" />
<label for="restore" class="btn"><i class="icon-home"></i> Restore Defaults</label>
<input id="restore" name="Submit" value="Restore Factory Defaults" type="submit" class="hidden" />
</p>
</form>
</body>
</html>''')
答案 0 :(得分:0)
部分问题在于你从头开始全力以赴。在更大的Python社区中提供了许多模板库和工具,您可能需要查看这些库和工具。就个人而言,我喜欢Flask。
如果我 HAD 在不使用外部库的情况下解决此问题,我会将代码中location
的所有实例更改为{location}
,然后添加{{ 1}}到最后。
.format(location = location)
答案 1 :(得分:0)
感谢cwallenpoole我正在取得一些进展。我正在设法从设置列表中提取一些信息。但是,我似乎无法影响单选按钮或下拉列表。这是我现在的代码:
#!/usr/bin/python
import config_settings
import cgi
import cgitb
cgitb.enable(display=0,logdir="/var/www/cgi-bin/error-logs")
print("Content-Type: text/html\n\n")
print("")
latitude = config_settings.settings[0]
latHemi = config_settings.settings[1]
longitude = config_settings.settings[2]
longHemi = config_settings.settings[3]
sensitivity = config_settings.settings[4]
htmlFormat='''<html>
<head>
<meta charset="utf-8">
<title>EM2010 Sound Level Monitor - Setup</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="EM2010 User Interface">
<meta name="author" content="Sonitus Systems">
</head>
<body>
<form class="well form-inline" method="post" action="/cgi-bin/process_setup.cgi">
<!-- Location -->
<i class="icon-location-arrow icon-large"> <span class="setting"> Location<span></span></i><br><br>
Latitude: <input type="text" name="latitude" class="input-small" value="{latitude}">°
<select name="latHemi">
<option selected="{latHemi}">N</option>
<option>S</option></select>
<option>N</option>
<option selected="{latHemi}">S</option></select>
Longitude: <input type="text" name="longitude" class="input-small" value="{longitude}">°
<select name="longHemi">
<option selected="{longHemi}">E</option>
<option>W</option></select>
<option>E</option>
<option selected="{longHemi}">W</option></select>
<hr/>
<!-- Mic Sensitivity -->
<i class="icon-microphone icon-large"> <span class="setting"> Microphone<span></span></i><br><br>
Sensitivity: <input type="text" name="sensitivity" class="input-small" value="{sensitivity}"> dB
print htmlFormat.format(latitude=latitude, latHemi=latHemi, longitude=longitude,
longHemi=longHemi, sensitivity=sensitivity,weight=weight)
有人能指出我正确的方向吗?