简体   繁体   English

Django forms,表格始终无效

[英]Django forms, form is always invalid

Yes, I did look at the other responses to similar questions.是的,我确实看过其他类似问题的回复。 But I haven't found one that helps me, Ive looked at all the currently available solutions.但是我还没有找到对我有帮助的,我查看了所有当前可用的解决方案。

I am trying to enter a name into a textbox, and then hit submit, allowing me to create a new list of items (the items are irrelevant).我正在尝试在文本框中输入一个名称,然后点击提交,允许我创建一个新的项目列表(这些项目是不相关的)。

But when I hit the submit button nothing happens.但是当我点击提交按钮时,什么也没有发生。 After many print statements, Ive deduced that the reason why is because the form.is_valid() function is returning false经过多次打印语句,我推断原因是因为form.is_valid() function 返回 false

if response.method == "POST":
    # returns a dictionary of information in the form
    form = CreateNewList(response.POST)

    print(form.errors)
    # if the form is valid, get the name attribute and create a new ToDoList with it
    if form.is_valid():
        n = form.cleaned_data["name"]
        t = ToDoList(name=n)
        t.save()

        return HttpResponseRedirect("/%i" % t.id)
else:
    form = CreateNewList()

return render(response, "main/create.html", {"form": form})

After reading some posts I found online, the next step I took was printing out the errors using forms.errors This is what I got from that print out在阅读了我在网上找到的一些帖子后,我采取的下一步是使用forms.errors打印出错误这就是我从该打印输出中得到的

<ul class="errorlist"><li>check<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

At this point, I have no clue what check is or does.在这一点上,我不知道检查是什么或做什么。 One persons response online said that it is part of some dictionary and I have to do something like form['check'] = 1 , but when I do that it says the dictionary is immutable or an invalid statement.网上有人回应说它是某些字典的一部分,我必须做类似form['check'] = 1事情,但是当我这样做时,它说字典是不可变的或无效的声明。 I also tried form.data['check']=1 , same thing我也试过form.data['check']=1 ,同样的事情

Here is the function that creates a new list这是创建新列表的 function

class CreateNewList(forms.Form):
    name = forms.CharField(label="Name", max_length=200)
    check = forms.BooleanField(label="Completed", required=True)

To get started with this issue, you should do the following:要开始解决此问题,您应该执行以下操作:

  • Replace response.method with request.method .response.method替换为request.method This is because you want the view to know what was the request's method, not the response's.这是因为您希望视图知道请求的方法是什么,而不是响应的方法。
  • Replace response.POST with request.POST .response.POST替换为request.POST This is because you want to populate data from previous POST request.这是因为您想从之前的POST请求中填充数据。

Back to the topic, your Boolean field is marked as a required field ( required=True ) and that's why you cannot pass the validation without having related checkbox checked.回到主题,您的 Boolean 字段被标记为必填字段 ( required=True ),这就是为什么如果不选中相关复选框就无法通过验证。 If you want the checkbox to be checked by default (on first GET request) and not to be required, use initial=True instead of required=True .如果您希望默认选中复选框(在第一个GET请求时)而不是必需的,请使用initial=True而不是required=True

Read more in related question .相关问题中阅读更多信息。

I got my desired outcome but I don't know how.我得到了我想要的结果,但我不知道如何。 Here is the chain of events:以下是事件链:

Dawid Mszanowski suggested that I change the value of required to False and set initial to true like this Dawid Mszanowski 建议我将 required 的值更改为 False 并将 initial 设置为 true 像这样

check = forms.BooleanField(label="Completed", initial=True)

And I tried this earlier and I was still getting errors, didnt work, but I kept the change而且我之前尝试过,但仍然出现错误,没有用,但我保留了更改

Then I implemented what MarkWalker suggested by implementing the check box into my User interface and allowing the user to manually check it off, this actually did fix the problem and I got what I was looking for.然后我通过在我的用户界面中实现复选框并允许用户手动检查它来实现 MarkWalker 的建议,这实际上确实解决了问题,我得到了我想要的东西。 Then I reverted all the changed Dawid suggested I make and my program was still running as intended so it seemed as though Marks solution fixed everything.然后我恢复了 Dawid 建议我所做的所有更改,并且我的程序仍然按预期运行,所以看起来 Marks 解决方案修复了所有问题。 But I didnt like how the user had to manually click the checkbox so I took that off.但我不喜欢用户必须手动单击复选框,所以我把它拿掉了。 I was pretty much back to square 1 at this point right?在这一点上,我几乎回到了第 1 格,对吧? I reverted the changes that Dawid suggested AND the changes that Mark suggested.我恢复了 Dawid 建议的更改和 Mark 建议的更改。 And now for some reason my program decides to work.现在由于某种原因我的程序决定工作。 Not sure what happened but hey, thanks guys I really appreciate it不知道发生了什么,但是嘿,谢谢大家,我真的很感激

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM