用于上载,编辑和验证的表单元素

时间:2019-02-12 09:26:07

标签: python python-3.x flask

我有一个包含很大表格的应用程序。为了使用户更容易,计划是将表单分解为复合部分(为方便起见,将其保留为2)。

到目前为止,我有以下代码:

我的问题是让section1用作初始表单(无ID),纠正验证错误的表单(重定向回)和供用户稍后编辑时链接回的表单(具有ID)。 / p>

下面显示了我的路由,但是这种方法很凌乱,如您所见,涉及将id传递到url中以引用现有记录时的辅助路由:

@scheme.route('/section1', methods = ['GET', 'POST'])
def section1():

    form = SchemeForm(request.form)
    if request.method == 'GET':
        return render_template('scheme/section1.html', form=form)

    if request.method == 'POST':
        if form.validate() == False:
            return render_template('scheme/section1.html', form=form)
        else:
            data = request.form.to_dict(flat=True)
            scheme_item = scheme_model().create(data)

            return redirect(url_for('.section2', id=scheme_item['id']))

@scheme.route('/<id>/section1', methods=['GET', 'POST'])
def section11(id):

    scheme_data = scheme_model().read(id)
    form = SchemeForm(request.form)

    if request.method == 'GET':
        return render_template(
            'scheme/section11.html',
            form=form,
            scheme_item=scheme_data
        )

    if request.method == 'POST':
        if form.validate() == False:
            return render_template('scheme/section11.html', form=form, scheme_item=scheme_data )
        else:
            data = request.form.to_dict(flat=True)
            scheme_data.update(data)
            scheme_item = scheme_model().update(scheme_data, id)
            return redirect(url_for('.section2', id=scheme_item['id']))

@scheme.route('/<id>/section2', methods = ['GET', 'POST'])
def section2(id):

    scheme_data = scheme_model().read(id)
    form = SchemeForm2(data=scheme_data)
    if request.method == 'GET':
        return render_template(
            'scheme/section2.html',
            form=form,
            scheme_item=scheme_data)

    if request.method == 'POST':
        if form.validate() == False:
            return render_template(
                'scheme/section2.html',
                form = form,
                scheme_item=scheme_data
            )
        else:
            data = request.form.to_dict(flat=False)
            scheme_data.update(data)
            scheme_item = scheme_model().update(scheme_data, id)
            return redirect(url_for('.section3', id=scheme_item['id']))

这是我的观点的一个例子:

{% extends "base.html" %}

{% block content %}
  <div>
    <div>
      <nav>
        <ol>
          <li class="current">
            <a data-nav-position="1 of 8: " href="section1?change=false">
              Section 1
              <span class="visuallyhidden"> - current step</span>
            </a>
          </li>

          <li class="inactive">
            <a data-nav-position="2 of 8: " href="section2?change=false">
              Section 2
            </a>
          </li>

          <li class="inactive">
            <a data-nav-position="3 of 8: " href="section3?change=false">
              Section 3
            </a>
          </li>

          <li class="inactive">
            <a data-nav-position="4 of 8: " href="section4?change=false">
              Section 4
            </a>
          </li>

          <li class="inactive">
            <a data-nav-position="5 of 8: " href="section5?change=false">
              Section 5
            </a>
          </li>

          <li class="inactive">
            <a data-nav-position="5 of 8: " href="section6?change=false">
              Section 6
            </a>
          </li>

          <li class="inactive">
            <a data-nav-position="5 of 8: " href="section7?change=false">
              Section 7
            </a>
          </li>

          <li class="inactive">
            <a data-nav-position="6 of 8: " href="review?change=false">
              Review
            </a>
          </li>

          <li class="show-all">
            <a style="visibility: visible;">
              Show all steps
            </a>
          </li>

        </ol>
      </nav>

      <h1>
        Section 1: Project management and strategic alignment
      </h1>

      <form action="section1" method="POST">
        {{ form.hidden_tag() }}

        <div>
          {{ form.ProjectName.label }}
          {% for message in form.ProjectName.errors %}
            <div>{{ message }}</div>
          {% endfor %}
          {{ form.ProjectName }}
        </div>
        <div>
          <input type=submit value=Submit>
        </div>
      </form>
    </div>
  </div>
{% endblock %}

0 个答案:

没有答案