I have a text submission box, a “category” dropdown and then three “sport” dropdowns (each sport dropdown containing a list of categorized sports. I want to submit all three forms with one input button (only submit the one sport drop down that has been selected). With this code the text and content forms submit fine as does the category but the sport selector doesn’t work it always has the postelement of “sea” which is the first group. What is wrong with my if elif statements??
<form method="post">
<div class="row">
<div class="span6">
<h4>post something!</h4>
<label>
<input type="text" placeholder="Subject" name="subject" value="{{subject}}">
</label>
<label>
<textarea style="height: 100px; width: 540px;" name="content">{{content}}</textarea>
</label>
{% if error %}
<div class="alert alert-error">Error: {{error}}</div>
{% endif %}
</div>
<div class="span2">
<h4>categories</h4>
<select name="postcategory">
<option>choose a category</option>
<option>general</option>
<option>discussion</option>
<option>adventures</option>
<option>review</option>
<option>badge applications</option>
</select>
</div>
<div class="span2 offset1">
<h4>choose element and/or sport</h4>
<select name="postsport">
<option value="general sea">--general sea--</option>
<option value="sailing">sailing</option>
<option value="diving">diving</option>
<option value="sailing">surfing</option>
<option value="kite boarding">kite boarding</option>
<option value="kayaking">kayaking</option>
<option value="general air">--general air--</option>
<option value="skydiving">skydiving</option>
<option value="paragliding">paragliding</option>
<option value="hang gliding">hang gliding</option>
<option value="base jumping">base jumping</option>
<option value="balloons">balloons</option>
<option value="general land">--general land--</option>
<option value="rock climbing">rock climbing</option>
<option value="hiking">hiking</option>
<option value="biking">biking</option>
<option value="skiing">skiing</option>
<option value="snowboarding">snowboarding</option>
</select>
</div>
</div>
<input class="btn" type="submit">
</form>
Here is my python to handle this information
postcategory = self.request.get(‘postcategory’)
postsport = self.request.get(‘postsport’)
postelement = “general”
if postcategory == "choose a category":
postcategory = "general"
if postsport == "sailing" or "diving" or "surfing" or "kite boarding" or "kayaking" or "general sea":
postelement ="sea"
elif postsport == "skydiving" or "paragliding" or "hang gliding" or "base jumping" or "balloons" or "general air":
postelement ="air"
elif postsport == "rock climbing" or "hiking" or "biking" or "skiing" or "snowboarding" or "general land":
postelement ="land"
else:
postelement="general"
postsport = "general"
This :
reads as :
In this case, since a non-empty string has a true value in a boolean context, and since the “or” operator returns the first operand having a true value, test will be bound to “sailing”. cf the following interactive session for an illustration:
The test you want is:
An even better solution would be to use a dict: