Reputation: 538
I'm have the following code for my app:
db.define_table('event',
Field('name'),
Field('date','date'),
Field('location'),
format='%(name)s')
db.define_table('presentations',
Field('event', db.event),
Field('title'),
Field('description'),
Field('level'))
db.define_table('presenters',
Field('name'),
Field('company'),
Field('resume'),
Field('avatar','upload),
format='%(name)s')
db.define_table('presentations_presenters',
Field('presentation', db.presentation),
Field('presenter', db.presenters))
Then I have created a controller for presentations:
def admpresentations():
presentationList=db(db.presentations.event==request.vars.eventid).select()
return dict(presentations=presentationList)
and in my view this is shown as:
{{for presentation in presentations:}}
{{=presentation.title}}
{{=presentation.description}}
{{=presentation.level}}
{{pass}}
Now, here's my question: I have to add presenters for each presentation. For this want to set a default value for presentations_presenters.presentation
, since there can be more than one presenter for each presentation, so I'm trying this in my view:
{{for presentation in presentations:}}
{{=presentation.title}}
{{=presentation.description}}
{{=presentation.level}}
<!--This is for setting a default value to presentation field, but doesn't work-->
{{db.presentations_presenters.presentation.default=presentation.id}}
<!--This creates the form, but default values are ignored-->
{{=crud.create(db.presentations_presenters}}
{{pass}}
tried with SQLFORM, but i can't figure out how to process it in view, so data isn't written into DB... any clues?
In last line:
{{=crud.create(db.presentations_presenters}}
setting default value does work, but data isn't saved into db.
Upvotes: 1
Views: 1509
Reputation: 101
Why would you want to create form in the view?
Simple make a SQLFORM or SQLFORM.factory in the controller and set default values using form.= default value.
Upvotes: 0
Reputation: 25536
crud.create()
creates a default _formname
hidden field in the form, which is used in conjunction with the _formkey
hidden field to protect agains CSRF attacks. Your code ends up creating multiple forms with the same name. As a result, the _formkey
value stored in the session will match only the last form created, so submissions of any of the earlier forms will be rejected due to a _formkey
mismatch. To get around this, you must give each form a unique name. For example:
{{=crud.create(db.presentations_presenters,
formname='presenters/%s' % presentation.id)}}
Also, if you want to use SQLFORM
instead, just call the .process()
method to process it in the view:
{{=SQLFORM(db.presentations_presenters).process(
formname='presenters/%s' % presentation.id)}}
Upvotes: 2