0

所以我在这个答案中看到:

http://stackoverflow.com/a/11072057/1061426

有人说:

更改此行:

form = StatementForm(request.POST, initial={'time': d.strftime("%Y-%m-%d %H:%M:%S"), 'user':loggedin_user, 'views':0})

为了这:

form = StatementForm(initial={'time': d.strftime("%Y-%m-%d %H:%M:%S"),'user':loggedin_user, 'views':0})

包含 request.POST 和不包含有什么区别?或者,更重要的是 - 如果值 X在 request.POST 中设置,但包含在初始数组中,那么 is_valid() 方法会看到 X 的什么值?

编辑:我想我要问的是〜在上面优先吗?如果添加了 request.POST 和初始值,初始值是否会覆盖 request.POST 值?是否可以覆盖“空”值?
(在我涉及的问题中,当方法是 get 时,作者错误地使用 request.POST 为 StatementForm 播种,这给他带来了问题。)

4

1 回答 1

2

initial is an argument used to set the initial value of the form at runtime.

Now, request.POST is used to bind a form to post data. For example, while submitting a form from browser using POST all the relevant fields would be assigned to the form object that it can find in request.POST

If X is not set in the form, the value is taken from the initial, if present.

You can read up about initial here

于 2012-09-26T19:15:25.923 回答