Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在提交表单后仅清除表单中的特定字段?_Python_Django - Fatal编程技术网

Python 如何在提交表单后仅清除表单中的特定字段?

Python 如何在提交表单后仅清除表单中的特定字段?,python,django,Python,Django,我有一个带有几个字段的表单。大多数情况下,人们每次提交时只需要更改一个字段,而不是所有字段,因此我希望以这样的方式进行更改:提交时,仅清除“员工”字段,但“工作区域选择”和“工作站”(如果有可用的话)保持选中状态 我如何改变表格的处理方式,使其在提交时不被完全清除?我知道清除它的是form=WarehouseForm(),但我不确定如何指定我只希望清除employee\u number字段 视图.py def enter_exit_area(request): enter_without

我有一个带有几个字段的表单。大多数情况下,人们每次提交时只需要更改一个字段,而不是所有字段,因此我希望以这样的方式进行更改:提交时,仅清除“员工”字段,但“工作区域选择”和“工作站”(如果有可用的话)保持选中状态

我如何改变表格的处理方式,使其在提交时不被完全清除?我知道清除它的是
form=WarehouseForm()
,但我不确定如何指定我只希望清除
employee\u number
字段

视图.py

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():
            emp_num = form.cleaned_data['employee_number']
            area = form.cleaned_data['work_area']
            station = form.cleaned_data['station_number']

            if 'enter_area' in request.POST:
                # Submission logic
                form = WarehouseForm()

            elif 'leave_area' in request.POST:
                # Submission logic ..

                form = WarehouseForm()
    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,
    })

如果它是输入型文本框,那么您也可以在javascript中执行,如下所示: 例如:
document.getElementById(“formFieldId”).value=''

您可以使用以下参数:

if 'enter_area' in request.POST:
    # Submission logic
    form = WarehouseForm(initial={'employee_number': '', 'work_area': area, 'station_number': station})
注意
员工编号
字段为空

您没有包含提交逻辑,因此您的实现可能略有不同。但原理是一样的。

检查以下线程:
if 'enter_area' in request.POST:
    # Submission logic
    form = WarehouseForm(initial={'employee_number': '', 'work_area': area, 'station_number': station})