How to create edit forms. For url edit?id=1121 I want to display pre-filled data
EditForm(twf.Form):
class child(twf.TableLayout):
name= twf.TextField(name="name",value=DBSession.query(student.name).filter(student.id == <id passed in url>).distinct().all())
@expose ('edit')
def edit(self, id)
return dict(page='edit', , form=EditForm(action='/save')
Template:
<div>${form.display()}</div>
There are a few ways to achieve this, but I'd say that the cleanest one is passing the values to the form from the controller action as showcased by http://turbogears.readthedocs.io/en/latest/turbogears/widgets_forms.html#displaying-forms
In the case of your specific example it should result in having a form that only declares the fields that need editing and a reference to the edited object id:
class EditForm(twf.Form):
class child(twf.TableLayout):
student_id = twf.HiddenField()
name = twf.TextField(name="name")
Then within the controller you can fetch the edited object and provide the values to the form:
@expose('edit')
def edit(self, id):
edited_student = DBSession.query(student).filter(student.id==id).first()
return dict(page='edit',
form=EditForm(action='/save',
value=dict(student_id=id,
name=edited_student.name))
Keep in mind that this is just a proof of concept, I haven't tested the code and it lacks proper validation/error handling. But it should pretty much give the idea that you just pass the student name to the form through the value
parameter when displaying it.