Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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 如何检查Django表单是否包含任何数据?_Python_Django - Fatal编程技术网

Python 如何检查Django表单是否包含任何数据?

Python 如何检查Django表单是否包含任何数据?,python,django,Python,Django,我有Django函数视图,里面有两个表单。检测哪个表单的数据由用户键入的最佳解决方案是什么?我知道form.is\u valid()是如何工作的,但我还是想先检查哪个表单中填充了数据 我的代码: def edit_profile_view(request): if request.method == "POST": edit_form = EditProfileForm(request.POST) pass_form = ChangePasswordFor

我有Django函数视图,里面有两个表单。检测哪个表单的数据由用户键入的最佳解决方案是什么?我知道
form.is\u valid()
是如何工作的,但我还是想先检查哪个表单中填充了数据

我的代码:

def edit_profile_view(request):
    if request.method == "POST":
        edit_form = EditProfileForm(request.POST)
        pass_form = ChangePasswordForm(request.POST)
        # here I want to detect which form has data inside,
        # and then save this form
    else:
        edit_form = EditProfileForm()
        pass_form = ChangePasswordForm()

    template = get_template("profiles/profile_edit.html")
    variables = RequestContext(request, {'edit_form': edit_form, 'pass_form': pass_form})
    output = template.render(variables)
    return HttpResponse(output)

只需为表单设置两个不同的操作/视图,那么包含数据的表单就是用户单击提交的表单

你可以,就像你说的,只是检查表单错误实际上是什么,或者在你的表单上为
is\u filled\u in
创建一个方法,但这一切对我来说似乎太过分了