【问题标题】:how to redirect a form error to the form page with validaton errors如何将表单错误重定向到包含验证错误的表单页面
【发布时间】:2019-03-12 03:42:57
【问题描述】:

我创建了一个包含用户详细信息的表单,并编写了一个类视图来更新我的 django aapp 中的用户详细信息,一切正常,但是,我通过该表单在页面中手动编码表单,而不是表单小部件,我正在更新详细信息。我不明白如何将表单错误从类视图重定向到有错误的表单页面,在 django 错误中它显示 不包含 aapp/user_form.html 模板

“profile.html”页面中包含表单的 HTML 代码

<div class="row">
        <div class="col-md-12">
            <form action="{% url 'updatedprofile' user.id %}" method="post">
                {% csrf_token %}
          <div class="modalBox">
          <div class="row">
            <div class="col-md-11">
              <h4>Update Profile</h4>
            </div>
            <div class="col-md-1 icon">
              <p><a href="{% url 'mprofile' %}"><i class="fas fa-times"></i></a></p>
            </div>
          </div>
          <div class="row">
            <div class="col-md-12">
              <table class="table table-striped">
                <thead>
                  <tr>
                    <td>First Name:</td>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>
                        <input type="text" name="first_name" value="{{user.first_name}}" class="form-control no-border simplebox" placeholder="Enter Your First Name Here...">
                    </td>
                  </tr>
                </tbody>
                <thead>
                  <tr>
                    <td>Last Name:</td>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>
                      <input type="text" name="last_name" value="{{user.last_name}}" class="form-control no-border simplebox" placeholder="Enter Your Last Name Here...">
                    </td>
                  </tr>
                </tbody>
              </table>
              <table class="table table-striped">
                <thead>
                  <tr>
                    <td>Mobile: </td>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td><input type="number" name="mobile" value="{{user.mobile}}" class="form-control no-border simplebox" placeholder="Enter Mobile Number Here..."></td>
                  </tr>
                </tbody>
              </table>
              <table class="table table-striped">
                <thead>
                  <tr>
                    <td>Profile Pic: </td>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td><input type="file" name="avatar" value="{{user.avatar}}" class="form-control no-border simplebox"></td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
          </div>
            <center><button type="submit" class="btn btn-success">Add Services</button></center>
                </form>
        </div>
      </div>

查看代码

class UpdateMprofile(LoginRequiredMixin, LogoutIfNotSuperuserMixin, UpdateView):
    login_url = reverse_lazy('mlogin')
    model = User
    fields = ['first_name', 'last_name', 'mobile', 'avatar']
    success_url = reverse_lazy('mprofile')

【问题讨论】:

    标签: django django-forms django-2.0


    【解决方案1】:

    正如我所见,您没有在视图中定义 template_name,因此您收到了模板错误。首先解决这个问题 -

    class UpdateMprofile(LoginRequiredMixin, LogoutIfNotSuperuserMixin, UpdateView):
        template_name = aapp/profile.html #Template directory should be correct.
        login_url = reverse_lazy('mlogin')
        model = User
        fields = ['first_name', 'last_name', 'mobile', 'avatar']
        success_url = reverse_lazy('mprofile')
    

    Django 已经在显示错误,但在您的模板中您没有对其进行编码以显示错误。

    一次显示所有错误,你可以 -

    {% for error in form.non_field_errors %}
        {{error}}
    {% endfor %}
    

    或者如果你想用他们的字段呈现错误,使用这个 -

       {% for error in form.first_name.errors %}
            <span class="help-block text-danger">{{ error }}</span>
        {% endfor %}
    

    另外一个建议,表格使用 - Widget Tweaks,这里是好的tutorial

    【讨论】:

    • 亲爱的 pankaj sharma 感谢您教我...尽管在基于课堂的视图中,如果出现任何错误,它会自动重定向到 succes_ url,但我现在清楚了,只有成功的方法 success_url 才有效
    【解决方案2】:

    当您使用通用视图时,您应该遵循 Django 模板命名。 在您的问题中,您遇到了模板错误,因此您应该添加带有_form 后缀的模板,例如yourmodelname_form.html。 Django 会自动在你的模板文件夹中搜索,Refer Official Document Here

    验证错误可以显示在,

    {{form.non_field_errors}}
    

    注意:最好使用forms.py进行编辑。

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 2018-08-03
      • 2020-09-04
      • 2013-10-13
      相关资源
      最近更新 更多