Crispy-form form.name呈现垃圾

时间:2014-05-21 12:55:51

标签: django django-views django-crispy-forms

我尝试渲染表单,但{{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" />">

如何追踪这个被破坏的地方?我有点怀疑它被打破了,因为形式可能会混淆这个名字&#39;具有form.name属性的字段。

2 个答案:

答案 0 :(得分: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 }}

答案 1 :(得分: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>