gpt4 book ai didi

python - 访问 Django 模板中的各个表单字段

转载 作者:行者123 更新时间:2023-11-30 23:30:56 27 4
gpt4 key购买 nike

我想访问表单的单个元素...这是代码:

模型.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'

View .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()

表单.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 的情况那样访问整个表单

任何帮助..

最佳答案

你可以这样做:

{% for form_field in add_gateway_details_form %}
{{ form_field }}
{% endfor %}

您还可以访问如下字段:

{{ add_gateway_details_form.gateway_name }}

这在the documentation中有详细解释。关于表单和模板。

关于python - 访问 Django 模板中的各个表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20241379/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com