def index():
"""
"""
response.flash = T("Hello World")
form = SQLFORM(db.QuarterMaster)
form.vars.PostDate = str(request.now.year) + "-" + str(request.now.month) + "-" + str(request.now.day)
if form.process().accepted:
form.vars.PostDate = request.vars.PostDate
response.flash = T("Thank you!")
elif form.errors:
response.flash = T("Please correct the errors in the form")
else:
response.flash = T("Please fill out the form")
return dict(form=form)
form = SQLFORM(db.QuarterMaster)
if request.vars.PostDate:If PostDate is a field of type date use
db.QuarterMaster.PostDate.default = request.now.date()
and remove all the
form.vars.PostDate assignments
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
form.vars appear only after form.process() call (process() - filter/convert/format/validate request.vars to form.vars)
you have variants :1. before form creation - modify field.default
2. before form.process() - modify request.vars - request.vars.PostDate=...
3. pass your own set of vars - form.process(vars=myvars)4. use form.process( onvalidation=myvalidation_fun) to modify form.vars after basic validation passed but before any db actions
It is always possible to pre-populate a form using the syntax:
1 | form.vars.name = 'fieldvalue' |
Pre-populating - means just default values which will be overwritten during form.process(), i.e. form.process() reads vars values only from request.post_vars or from 'vars' arg - form.process(vars = {...} ) and ignores pre-sets like form.vars.name=...
There was a mistake in my post: instead request.vars must be request.post_vars - I fixed this
The problem is that one controller is used for generation and process form. it's like different modespre-populating form by assignments form.vars.name=... could be used only for the request form, when form is submitted these assignments have no effect.if you want to change anything in the form after it was processed, you have to use DOM-parsing ( something like form[0].element('input', _name='...')['_value'] = ... )Use keepvalues=True or db.table.field.default as suggested above (it does the pre-populating during form.process())There is brute force variant - self-redirection : if form.process().accepted: ... session.flash='OK'; redirect(URL()) - but don't forget about args/vars/user_signature if it matters
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/hT-z8wbGr38/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
The problem is that one controller is used for generation and process form. it's like different modespre-populating form by assignments form.vars.name=... could be used only for the request form, when form is submitted these assignments have no effect.if you want to change anything in the form after it was processed, you have to use DOM-parsing ( something like form[0].element('input', _name='...')['_value'] = ... )
Use keepvalues=True or db.table.field.default as suggested above (it does the pre-populating during form.process())
There is brute force variant - self-redirection : if form.process().accepted: ... session.flash='OK'; redirect(URL()) - but don't forget about args/vars/user_signature if it matters
import datetime
response.flash = T("Hello World")
if request.post_vars.PostDate:
try:
db.QuarterMaster.PostDate.default = datetime.datetime.strptime(request.post_vars.PostDate, "%Y-%m-%d")
except ValueError:
pass
else:
db.QuarterMaster.PostDate.default = request.now.date()
# str(request.now.year) + "-" + str("%02d" % (request.now.month)) + "-" + str("%02d" % (request.now.day))
form = SQLFORM(db.QuarterMaster)
if form.process().accepted:
response.flash = T("Thank you!")
elif form.errors:
response.flash = T("Please correct the errors in the form")
else:
response.flash = T("Please fill out the form")
return dict(form=form)