我想访问表单的单个元素... 这是代码:
Models.py
class GatewayDetails(models.Model):
gateway_id = models.IntegerField(primary_key=True)
gateway_name = models.CharField(max_length=256L)
class Meta:
db_table = 'gateway_details'
class GatewayParameters(models.Model):
gateway = models.ForeignKey(GatewayDetails)
id=models.IntegerField(primary_key=True)
gateway_param1 = models.CharField(max_length=256L)
gateway_param2 = models.CharField(max_length=256L)
gateway_param3 = models.CharField(max_length=256L)
gateway_param4 = models.CharField(max_length=256L)
gateway_param5 = models.CharField(max_length=256L)
gateway_param6 = models.CharField(max_length=256L)
gateway_param7 = models.CharField(max_length=256L)
gateway_param8 = models.CharField(max_length=256L)
gateway_param9 = models.CharField(max_length=256L)
class Meta:
db_table = 'gateway_parameters'
views.py
if request.method=="POST":
add_gateway_details_form=Add_Gateway_Details(request.POST)
if add_gateway_form.is_valid():
success=True
else:
add_gateway_details_form=Add_Gateway_Details()
else:
add_gateway_details_form=Add_Gateway_Details()
add_gateway_parameters_form=Add_Gateway_Parameters()
forms.py
class Add_Gateway_Details(forms.ModelForm):
class Meta:
model=GatewayDetails
exclude=('gateway_id',)
class Add_Gateway_Parameters(forms.ModelForm):
class Meta:
model=GatewayParameters
exclude=('gateway',)
HTML:
<form action="." method="POST">
{{ add_gateway_details_form.as_p}}
{{ add_gateway_parameters_form.as_p }}
<input type="submit" value="send">
</form>
我想访问表单的各个元素(例如:gateway_name,gateway_param1)而不是整个表单,例如add_gateway_details_form.as_p或add_gateway_parameters_form.as_p
任何帮助..
答案 0 :(得分:1)
你可以这样做:
{% for form_field in add_gateway_details_form %}
{{ form_field }}
{% endfor %}
您还可以访问以下字段:
{{ add_gateway_details_form.gateway_name }}
在表格和模板的the documentation中对此进行了详细说明。
答案 1 :(得分:1)
实际上,在表格之外你可能需要这个:
{{some_form.some_field.value}}