如何将表单错误重定向到具有验证错误的表单页面

how to redirect a form error to the form page with validaton errors

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

Html "profile.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')

据我所知,您没有在视图中定义 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.

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

验证错误可以显示在

{{form.non_field_errors}}

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