0

So i saw in this answer:

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

that someone said:

Change this line:

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

For this:

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

What is the difference between including the request.POST and not? Or, more to the point - if value X isn't set in request.POST, but is included in the initial array, what value of X does the is_valid() method see?

EDIT: I guess what i'm asking is ~ which takes precedence in the above? If request.POST and an initial are added, does the initial value overwrite the request.POST value? Is an "empty" value able to be overwritten?
(In the question i relate to, the author was mistakingly using request.POST to seed the StatementForm when the method was a get, which was causing problems for him. )

bharal
  • 15,461
  • 36
  • 117
  • 195

1 Answers1

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

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • but this doesn't answer the question... I've edited to be clearer! – bharal Sep 26 '12 at 22:33
  • form has an attribute has_changed. If something has changed in the POST request, it takes that, if not it takes the initial value, if present. Just try it for yourself. you will know.. It is a really simple concept. – karthikr Sep 26 '12 at 23:41