【发布时间】:2016-05-13 13:46:33
【问题描述】:
我有两个模型:
class Location(models.Model):
location_id=models.AutoField(primary_key=True)
location=models.CharField(max_length=30, blank=False, null=False)
phone_code=models.CharField(max_length=10,blank=True, null=True)
def __str__(self):
return self.location
class Host(models.Model):
host_id=models.AutoField(primary_key=True)
location=models.ForeignKey('Location', on_delete=models.CASCADE)
host=models.CharField(max_length=30, blank=False, null=False)
def __str__(self):
return "%s. %s" % (self.location.location, self.host)
这是我模板的视图:
def index(request):
locations=Location.objects.order_by('location')
hosts=Host.objects.order_by('host')
template=loader.get_template('ports/index.html')
return HttpResponse(template.render({'locations':locations, 'hosts':hosts},request))
我想在模板中放置两个输入选择,如下所示:
<select class="form-control" id="location">
{% for location in locations %}
<option>{{location.location}}</option>
{% endfor %}
</select>
<select class="form-control" id="host">
?????? dynamic update depending on location's select
</select>
第二个选择将包含主机。当我更改位置选择的值时,我需要主机的选择动态更新。
【问题讨论】:
-
好的,我可以使用 select.onChange。但是如何通过 Jquery 获取主机值?
标签: django model python-3.4