2013-11-27 60 views
0

我要訪問的形式的單個元素... 這裏是代碼:訪問各個表單字段

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

任何幫助的情況下。 。

回答

1

你可以這樣做:

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

您也可以訪問像場:

{{ add_gateway_details_form.gateway_name }} 

這在窗體和模板上的the documentation中有詳細說明。

0

其實,形式外,你可能需要這樣:

{{some_form.some_field.value}}