Loading data to ToscaWidgets2 forms

9 views
Skip to first unread message

Mikhail

unread,
May 7, 2019, 10:48:05 AM5/7/19
to TurboGears
Hello again!
Finally I have time to port my project to new version of TurboGears. I was able to solve most of the issues but now I need your help:
I can't load data to TW2 form. Here are some code:

widgets/news.py fragment (form):

class NewsEditForm(TableForm):
    title_label
= Label(text=u'Изменить новость')
    tf_title
= TextField(label=u'Заголовок', validator=NotEmpty, size=50)
    tf_url
= TextField(label=u'Ссылка', validator=URL, size=50)
    tf_text
= TextArea(label=u'Текст новости', validator=NotEmpty, rows=10, cols=50)
    news_id
= HiddenField('news_id')

    submit
= SubmitButton(value=u"Изменить")
    action
= '/manage/saveNews'
   
manage.py Controller fragment:

class ManageController(BaseController):
...
   
@expose('cafealpha.templates.manage_news')
   
def news(self, news_id='', *args, **kw):
       
if not request.identity:   #if not authorized then redirect to /
            redirect
('/')

        news
= DBSession.query(News).filter_by(id=news_id).one()
        news_form
= NewsEditForm()
        news_form
.tf_title.value = news.title

       
return dict(pagename=u'Новости', form=news_form)

manage_news.xhtml fragment:

${form.display()}

Error I'm getting is:
  File "/home/uni/projects/cafealpha-tg23/venv/cafealpha/cafealpha/controllers/manage.py", line 46, in news
    news_form
.tf_title.value = news.title
AttributeError: type object 'NewsEditForm_d' has no attribute 'tf_title'


It seems I'm misssing something obvious so pleas point me.

My Python is rusty so feel free to laugh :)

P.S. I was able to load data to form using this code in template:
${form.display(value=dict(tf_url=news_url))}

and passing news_url with return dict() from Controller. But I just think this is not the best way. Or is it?

Alessandro Molina

unread,
May 11, 2019, 3:42:09 AM5/11/19
to TurboGears
You actually did right in your second experiment.
The way you are supposed to do it is by passing the `value` to the form, not trying to set attributes of single fields.

So in practice the right way would be to do:

form(value=dict(tf_url=X)).display()

or

form.display(value=dict(tf_url=X))

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.
To post to this group, send email to turbo...@googlegroups.com.
Visit this group at https://groups.google.com/group/turbogears.
To view this discussion on the web visit https://groups.google.com/d/msgid/turbogears/564ca3c2-0b22-4c30-8055-9b80ec542e41%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages