如何在django中正确显示表单错误消息

1cklez4t  于 2023-03-04  发布在  Go
关注(0)|答案(2)|浏览(138)

在这里与此代码的形式错误显示不正确。所有的错误消息说,这个字段是必需的only.how我可以定义我的自定义形式。错误消息在模板中。
模板

{% if form.errors %}
    {% for field in form %}
        {% for error in field.errors %}
            <p> {{ error }} </p>
        {% endfor %}
    {% endfor %}
{% endif %}
      <div class="form-group">
                        <h5>Full Name <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="text" name="name" class="form-control" title="Full Name is required" > </div>
                    </div>
                    <div class="form-group">
                        <h5>Courses<span class="text-danger">*</span></h5>
                           <div class="controls">
                     {% for course in courses %}
                     <input name ="courses" type="checkbox" id="course-{{course.id}}" value="{{course.id}}" autofocus  title="Please Check at least one course">
                     <label for="course-{{course.id}}">{{course.title}}</label>
                     {% endfor %}
                   </div>
                    </div>

views.py

if request.method == 'POST':
        form = AddStudentForm(request.POST, request.FILES)
        if form.is_valid():
            student = form.save(commit=True)

            student.save()

            messages.success(request, 'student with name  {}  added.'.format(student.name))
            return redirect('admin:add_student')

    else:
        form = AddStudentForm()
    return render(request, 'admin/add_student.html', {'form': form})
laik7k3q

laik7k3q1#

在您的views.py传递中,表单包含如下错误

if request.method == 'POST':
    form = AddStudentForm(request.POST, request.FILES)
    if form.is_valid():
        student = form.save(commit=True)

        student.save()

        messages.success(request, 'student with name  {}  added.'.format(student.name))
        return redirect('admin:add_student')

    return render(request, 'admin/add_student.html', {'form': form}) 
else:
    form = AddStudentForm()
    return render(request, 'admin/add_student.html', {'form': form})

这意味着您应该删除模板代码中的else部分和

{% if form.errors %}
    <div class="alert alert-danger">
        {{ form.errors }}
    </div>
{% endif %}

已更新答案以在首次加载视图时获得正确答案

wooyq4lh

wooyq4lh2#

您可以使用Django消息框架来实现这一点。
在您的views.py文件中,首先导入消息
from django.contrib import messages
然后做这样的事

if request.method == 'POST':
        form = AddStudentForm(request.POST, request.FILES)
        if form.is_valid():
            student = form.save(commit=True)
            student.save()
            messages.success(request, 'student with name  {}  added.'.format(student.name))
            return redirect('admin:add_student')
        else:
            for error in form.errors:
            messages.error(request, sign_in_form.errors[error])
            return redirect(request.path)
else:
    form = AddStudentForm()
    return render(request, 'admin/add_student.html', {'form': form})

在HTML文件中,访问类似以下内容的消息

{% if message.tags == 'error' %}
            <!-- bootstrap alert -->
            <div class="alert alert-warning alert-dismissible fade show" role="alert">
                <strong>{{ message }}</strong>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        {% endif %}
        {% if message.tags == 'success' %}
            <!-- bootstrap alert -->
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <strong>{{ message }}</strong>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        {% endif %}
    {% endfor %}

相关问题