下拉框
<select>
<option value=‘1’>北京</option>
<option value=“2”>上海</option>
</select>
这样写比较死,要灵活一些需要用到for循环
服务器端代码:
hello.py
def hi(request):
beijin = {"id":1,"dd":"北京"}
shanghai = {"id":2,"dd":"上海"}
userAreas = [beijin,shanghai]
dataset = {‘result‘:‘必须输入数字‘,"areas":userAreas}
return render_to_response("index.html",dataset)
返回一个字典
html代码:
<style>
.bb{border:2px solid red ;width: 600px;margin: 150px auto}
.textcenter{text-align: center}
.full{width:100%}
div{line-height:40px}
</style>
<form method="POST">
<div class="bb textcenter">
<div class="full">
请输入用户名:<input type="text" name="txtUserName"/>
</div>
<div class="full">
请输入密 码:<input type="password" name="txtUserPass"/>
</div>
<div class="full">
选择地区:<select name="userArea">
<option value="0">--请选择--</option>
{% for aa in areas %}
<option value="{{aa.id}}">{{aa.dd}}</option>
{% endfor %}
</select>
</div>
<div class="full">
<input type="submit" value="登录"/>
</div>
</div>
</form>
使用了for循环来遍历字典
本文出自 “dba天空” 博客,请务必保留此出处http://9425473.blog.51cto.com/9415473/1702102
原文:http://9425473.blog.51cto.com/9415473/1702102