为什么我的ValidationErrors不再呈现?

时间:2019-11-20 19:27:49

标签: python html django

我有一个表单,必须在提交前检查一些内容以确保数据有效。在进行一些更改(从基于类的视图到基于函数的更改)之前,一切都工作正常,但是当我重新测试所有内容时,我注意到一个非常重要的部分无法正常工作,即数字验证。

以前曾经发生过的事情是,如果输入了数据库中没有的数字,则会在顶部向用户显示错误。如果他们输入的是数据库中的数字,但不是正确的“团队”,那么它将显示错误。我在forms.py中处理此问题,但由于它之前一直在工作,所以完全忘记了它,而我开始使用的所有东西都在views.py中。现在,尽管它实际上不会提交表单(因此仍在通过逻辑操作),但它不会显示任何错误。它将仅重新呈现页面,但为空白,除非输入的数据正确,否则将不提交任何内容。处理此问题的逻辑是clean_employee_number。

我不确定是否在未注意到的情况下从html中删除了某些内容,但是我一直在尝试找出可能导致此功能停止工作的原因,而我无法弄清楚。我也不知道是否可能是由于窗口小部件所致,因为我之后进行了更改(这是一个窗口小部件,因为employee_number与存储在另一个称为Salesman的模型中的员工的“ id”相关联)。 / p>

models.py

class EmployeeWorkAreaLog(TimeStampedModel, SoftDeleteModel, models.Model):
    employee_number = models.ForeignKey(Salesman, on_delete=models.SET_NULL, help_text="Employee #", null=True, blank=False)
    work_area = models.ForeignKey(WorkArea, on_delete=models.SET_NULL, null=True, blank=False, help_text="Work Area", related_name="work_area")
    station_number = models.ForeignKey(Station, on_delete=models.SET_NULL, null=True, help_text="Station", related_name="stations", blank=True)

forms.py

class WarehouseForm(AppsModelForm):
    class Meta:
        model = EmployeeWorkAreaLog
        widgets = {
            'employee_number': ForeignKeyRawIdWidget(EmployeeWorkAreaLog._meta.get_field('employee_number').remote_field, site, attrs={'id':'employee_number_field'}),
        }
        fields = ('employee_number', 'work_area', 'station_number', 'edited_timestamp')

    def clean_employee_number(self):
        employee_number = self.cleaned_data.get('employee_number')

        if employee_number is None:
            raise forms.ValidationError("Must enter emp #")
        elif employee_number.team is None:
            raise forms.ValidationError("Not valid")
        elif employee_number.team not in ('WF', 'WP', 'OM') or employee_number.employee_status not in 'A':
            raise forms.ValidationError("Employee not valid, please contact manager")

views.py

def enter_exit_area(request):
    form = WarehouseForm(request.POST or None)
    enter_without_exit = None
    exit_without_enter = None

    if request.method == 'POST':
        temp = request.POST.copy()
        form = WarehouseForm(temp)
        if form.is_valid():
            emp_num = form.cleaned_data['employee_number']
            area = form.cleaned_data['work_area']
            station = form.cleaned_data['station_number']
            edited_time = form.cleaned_data['edited_timestamp']

            if 'enter_area' in request.POST:
                new_entry = form.save()
                EmployeeWorkAreaLog.objects.filter((Q(employee_number=emp_num) & Q(work_area=area) & Q(time_out__isnull=True) & Q(time_in__isnull=True)) & (Q(station_number=station) | Q(station_number__isnull=True))).update(time_in=datetime.now())

                # If employee has an entry without an exit and attempts to enter a new area, mark as an exception 'N'
                enters_without_exits = EmployeeWorkAreaLog.objects.filter(Q(employee_number=emp_num) & Q(time_out__isnull=True) & Q(time_exceptions="")).exclude(pk=new_entry.pk).order_by("-time_in")
                if len(enters_without_exits) > 0:
                    enter_without_exit = enters_without_exits[0]
                    enters_without_exits.update(time_exceptions='N')

                message = 'You have entered %(area)s' % {'area': area}
                if station is not None:
                    message += ': %(station)s' % {'station': station}
                messages.success(request, message)

            elif 'leave_area' in request.POST:
                # Something similar to above

    form = WarehouseForm()
    return render(request, "operations/enter_exit_area.html", {
        'form': form,
        'enter_without_exit': enter_without_exit,
        'exit_without_enter': exit_without_enter,
    })

enter_exit_area.html

{% extends "base.html" %}


{% block main %}
    <form id="warehouseForm" action="" method="POST" novalidate >
        {% csrf_token %}

        {{ form.non_field_errors }}
        {{ form.source.errors }}
        {{ form.source }}

        <div>
            <div>
                <div>{{ form.employee_number.errors.as_text }}</div>
                <label>Employee #</label>
                {{ form.employee_number }}
            </div>

            <div>
                <div>{{ form.work_area.errors.as_text }}</div>
                <label>Work Area</label>
                {{ form.work_area }}
            </div>
            <div>{{ form.station_number.errors.as_text }}</div>
            <div>
                <label>Station</label>
                {{ form.station_number }}
            </div>
        </div>

        <div>
            <div>
                <button type="submit" name="enter_area" value="Enter">Enter Area</button>
                <button type="submit" name="leave_area" value="Leave">Leave Area</button>
            </div>
        </div>
    </form>

1 个答案:

答案 0 :(得分:1)

您构造一个 new 表单,以防它是POST请求并且该表单无效。您的enter_exit_area的工作流程应为:

def enter_exit_area(request):
    enter_without_exit = None
    exit_without_enter = None
    if request.method == 'POST':
        form = WarehouseForm(request.POST)
        if form.is_valid():
            # …
            return redirect('some-view')
    else:
        form = WarehouseForm()
    return render(request, "operations/enter_exit_area.html", {
        'form': form,
        'enter_without_exit': enter_without_exit,
        'exit_without_enter': exit_without_enter,
    })

因此请注意,form = WarehouseForm()是在else if request.method == 'POST' 块中构造的。

如果POST请求成功,通常会进行 redirect 来实现Post/Redirect/Get pattern [wiki]