Mark Kelly Mark Kelly - 4 months ago 24
HTML Question

Passing a list to HTML form Python

I am trying to pass a list into an HTML form with Python. I am a noob and I am not really sure what I am doing so any advice would be appreciated.

What I am trying to do is fill in all the blank text boxes and click radio buttons and drop down lists / menus using the list. This list will be the default values for the form.

form = cgi.FieldStorage()
latitude = form.getvalue('latitude', '0')
if config_settings.settings[0]:
latitude = config_settings.settings[0]


I have been trying to do this with the CGI module but I am not doing this right. Should I use mechanise or selenium instead, or can this be done with CGI and FieldStorage. Any advice would be greatly appreciated.

#!/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">&nbsp;Location<span></span></i><br><br>
Latitude: <input type="text" name="latitude" class="input-small" value="lat">&deg;



<select name="latHemi">

<option selected="selected">N</option>
<option>S</option></select>

<option>N</option>
<option selected="selected">S</option></select>



&nbsp;&nbsp;
Longitude: <input type="text" name="longitude" class="input-small" value="$long">&deg;

<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">&nbsp;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">&nbsp;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 &nbsp;&nbsp;&nbsp;</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 &nbsp;&nbsp;&nbsp;</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 &nbsp;&nbsp;&nbsp;</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 &nbsp;&nbsp;&nbsp;</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 &nbsp;&nbsp;&nbsp;</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 &nbsp;&nbsp;&nbsp;</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 &nbsp;&nbsp;&nbsp;</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 &nbsp;&nbsp;&nbsp;</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 &nbsp;&nbsp;&nbsp;</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 &nbsp;&nbsp;&nbsp;</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) &nbsp;&nbsp;&nbsp;</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)&nbsp;&nbsp;&nbsp;</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">&nbsp;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">&nbsp;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>''')

Answer

Part of the problem is that you're spinning this all up from the ground up. There are many templating libraries and tools offered in the greater Python community, you may want to look at those. Personally, I like Flask.

If I HAD to solve this problem without recourse to an external library, I would change all instances of location in your code to {location} and then add a .format(location = location) to the end.

location = 'cat'
# Notice the location with braces, and the one without.
html = '<input value="{location}" name="location" type="text" />'
print(html.format(location = location))

# outputs <input value="cat" name="location" type="text" />
# the location with braces is replaced.