重构一个手动渲染字段的Django表单

时间:2018-02-20 22:23:38

标签: html django

我正在开发一个代码库,其中包含按https://docs.djangoproject.com/en/2.0/topics/forms/#rendering-fields-manually行编写的表单。以下是表单模板的片段,它使用Django Widget Tweaks

{% load widget_tweaks %}
{% csrf_token %}

<div class="row">
  <div class="input-field col s12">
    {{ form.session_number|add_error_class:"invalid" }}
    {% if form.session_number.errors %}
      <span id="{{ form.session_number.id_for_label }}-error" class="error">{{ form.session_number.errors|join:", " }}</span>
    {% endif %}
    <label for="{{ form.session_number.id_for_label }}"
        class="{% if form.session_number.value %}active{% endif %} {% if form.name.errors %}invalid{% endif %}">
      Session Number
    </label>
  </div>
</div>

<div class="row">
  <div class="col s12">
    <label for="email">Scheduled For</label>
  </div>
  {{ form.scheduled_for }}
</div>

{% if form.family.value %}
  <input name="family" required="" id="id_family" type="hidden" value="{{ form.family.value }}" />
{% else %}
  <div class="row">
    <div class="input-field col s12">

      {{ form.family|add_error_class:"invalid" }}
      {% if form.family.errors %}
        <span id="{{ form.family.id_for_label }}-error" class="error">
          {{ form.family.errors|join:", " }}
        </span>
      {% endif %}
      <label for="{{ form.family.id_for_label }}" class="{% if form.family.errors %}invalid{% endif %}">
        Family
      </label>
    </div>
  </div>
{% endif %}

<div class="row">
  <div class="input-field col s12">
    {{ form.expert|add_error_class:"invalid" }}
    {% if form.expert.errors %}
      <span id="{{ form.expert.id_for_label }}-error" class="error">
        {{ form.expert.errors|join:", " }}
      </span>
    {% endif %}
    <label for="{{ form.expert.id_for_label }}" class="{% if form.expert.errors %}invalid{% endif %}">
      Expert
    </label>
  </div>
</div>

<div class="row">
  <div class="input-field col s12">
    {{ form.session_type|add_error_class:"invalid" }}
    {% if form.session_type.errors %}
      <span id="{{ form.session_type.id_for_label }}-error" class="error">
        {{ form.session_type.errors|join:", " }}
      </span>
    {% endif %}
    <label for="{{ form.session_type.id_for_label }}" class="{% if form.session_type.errors %}invalid{% endif %}">
      Session Type
    </label>
  </div>
</div>

<div class="row">
  <div class="input-field col s12">
    {{ form.status|add_error_class:"invalid" }}
    {% if form.status.errors %}
      <span id="{{ form.status.id_for_label }}-error" class="error">
        {{ form.status.errors|join:", " }}
      </span>
    {% endif %}
    <label for="{{ form.status.id_for_label }}" class="{% if form.status.errors %}invalid{% endif %}">
      Status
    </label>
  </div>
</div>

以下是表单的相应部分的外观:

enter image description here

正如您所看到的,代码远离DRY,django.forms.Form类和模板之间也存在耦合:无论何时向前者添加字段,都需要添加相应的{{1到模板。

我正在考虑是否使用例如https://docs.djangoproject.com/en/2.0/ref/forms/renderers/#the-low-level-render-api中描述的自定义渲染器来重构此代码。然而,我想知道这是否值得,因为像https://simpleisbetterthancomplex.com/article/2017/08/19/how-to-render-django-form-manually.html这样的博客实际上描述了如何通过手动渲染字段来获得对表单的更多控制。

我的问题是:是否可以使用简单的模板获取表单的“外观和感觉”,例如

<div class="row">

使用适当的表单渲染器?或者还有其他方法可以减少代码重复吗?

0 个答案:

没有答案