2014-05-21 95 views
0

我想呈現一個表單,但{{form.name}}呈現垃圾。問題是,我甚至無法調試這個來判斷事物變形的地方。脆皮表單form.name呈現垃圾

形式:

class ActionItemForm(forms.ModelForm): 

    class Meta: 
     model = ActionItem 
     fields = ('project', 'portfolio', 'name', 'description', 'resolution', 'parent', 'priority', 'status', 'assignee', 'due_date') 

    def __init__(self, *args, **kwargs): 
     self.helper = FormHelper() 
     self.helper.form_tag = False 
     self.helper.layout = Layout() 
     super(ActionItemForm, self).__init__(*args, **kwargs) 

的視圖:

def action_item_actions(request, action_item_id, action_code): 
    def enrich_form(wf_item, form): 
     form.id = 'edit_action_item' 
     form.name = form.id 
     form.title = wf_item.name 
     form.action = '/forms/action_item/edit/%s' % action_item_id 
     return form 
    action_item = ActionItem.objects.get(id=action_item_id) 
    wf_item = action_item.wf_get_action_by_code(action_code) 
    if wf_item: 
     if wf_item.trans_to: 
      form = ActionItemForm(instance=action_item, initial={action_item.get_wf_field(): type(getattr(action_item, action_item.get_wf_field())).objects.get(id=wf_item.trans_to)}) 
     else: 
      form = ActionItemForm(instance=action_item) 
     form = enrich_form(wf_item, form) 
     for field in form.Meta.fields: 
      if field not in wf_item.fields: 
       form.helper.layout.fields.append(Field(field, type='hidden')) 
     return render_to_response('forms/modal_form.html', {'form': form}, template.RequestContext(request)) 
    else: 
     return render_to_response('forms/message.html', {'message': Message('Error', 'WF Descriptor not found')}, template.RequestContext(request)) 

當窗體存在認爲,form.name是正確的:

view debug displaying form.name is fine

現在,當表單進入模板,仍然可以:

template debug view displaying the form.name is still fine

的觀點很簡單:

{% load crispy_forms_tags %} 
<form id="{{ form.id }}" role="form" class="small" name="{{ form.name }}"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
     <h4 class="modal-title" id="myModalLabel">{{ form.title }}</h4> 
     </div> 
     <div class="modal-body"> 
     {% crispy form %} 
     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     <input type="button" class="btn btn-primary" value="Save" id="" onclick="submit_form('{{ form.id }}','{{ form.action }}')"> 
     </div> 
    </div> 
    </div> 
</form> 

但是,窗體標籤呈現爲這樣:

<form id="edit_action_item" role="form" class="small" name="<input id="id_name" maxlength="256" name="name" type="text" value="Test action item" />"> 

我如何追查,其中該得到打破?我懷疑它會被破壞,因爲表單可能會混合'name'字段和form.name屬性。

回答

0

您的表單中有name字段。所以{{ form.name }}返回字段名稱的輸出。你可以把表格名其他變量__init__

def __init__(self, *args, **kwargs): 
    self.helper = FormHelper() 
    self.helper.form_tag = False 
    self.helper.layout = Layout() 
    super(ActionItemForm, self).__init__(*args, **kwargs) 
    self.form_name = self.name # this line added 

,並在模板調用{{ form.form_name }}

0
<section class="form"> 
    <h5>Contact Form</h5> 
    <h6>Let Us Know</h6> 
    <form action="{% url 'web:contact' %}" method="post" class="ajax"> 
     {% csrf_token %} 
     <p class="left"> 
      <label for="{{form.name.label.id_for_label}}"> 
       {{form.name.label}} 
       {% if form.name.field.required %} 
        <small class="star">*</small> 
       {% endif %} 
      </label> 
      {{form.name}} 
      {% if form.name.errors %} 
       <span class="error">{{form.name.errors.as_text}}</span> 
      {% endif %} 
     </p> 
    </form> 
</section>