在自定义模板中显示django模型数据
我有像这样的django型号在自定义模板中显示django模型数据,django,django-models,django-templates,Django,Django Models,Django Templates,我有像这样的django型号 class District(models.Model): name = models.CharField(max_length=30,unique=True) number = models.PositiveIntegerField(null=True,blank=True) def __unicode__(self): return "District (%s,%s)" % (self.name,self.number) class Meta:
class District(models.Model):
name = models.CharField(max_length=30,unique=True)
number = models.PositiveIntegerField(null=True,blank=True)
def __unicode__(self):
return "District (%s,%s)" % (self.name,self.number)
class Meta:
db_table = 'districts'
我可以输入地区名称和编号。我想能够做的是查看在我创建的自定义模板中输入的这些地区。当前,模板有一个使用select标记的下拉选项。如何才能提取使用django模型输入的数据,并将其显示在我创建的模板中。这是html模板中的当前代码片段
<label for="district"> District</label>
<select id="district" name="district">
<option id="kampala" value="k">Kampala</option>
<option id="mbale" value="m">Mbale</option>
</select>
地区
坎帕拉
姆巴尔
在视图中,将区域对象传递到视图的上下文,如下所示:
districts = Districts.objects.all()
return render_to_response('mytemplate.html',{'districts': districts})
然后在模板(mytemplate.html)中执行以下操作:
<label for="district"> District</label>
<select id="district" name="district">
{% for dist in districts %}
<option id="{{ dist.name }}" value="{{ dist.number }}">{{ dist.name }} </option>
{% endfor %}
</select>
地区
{地区中的地区百分比%}
{{dist.name}
{%endfor%}
我希望有帮助 在视图中,将区域对象传递到视图的上下文,如下所示:
districts = Districts.objects.all()
return render_to_response('mytemplate.html',{'districts': districts})
然后在模板(mytemplate.html)中执行以下操作:
<label for="district"> District</label>
<select id="district" name="district">
{% for dist in districts %}
<option id="{{ dist.name }}" value="{{ dist.number }}">{{ dist.name }} </option>
{% endfor %}
</select>
地区
{地区中的地区百分比%}
{{dist.name}
{%endfor%}
我希望有帮助 哦,你必须把
名字
和数字
代替dist\u名字
和dist\u数字
。哦,你必须把名字
和数字
代替dist\u名字
。