[Twisted-web] two questions abt nevow
jason.mobarak at gmail.com
Mon Apr 25 17:34:53 MDT 2005
On 4/25/05, Little <mordaha at gmail.com> wrote:
> I'm newbie now in Model-View-Controller pattern, from perl/php it's
> difficult to think in DOM-model :)
> So i have two questions, answers on which i can't google:
> Q1: I have a page. At page i have 2 elements. The values(data->render)
> of both elements must calculated from single request to the Database.
> Simple, it's looks like:
> [total: 100 rows]
> [table-like result]
> So, in the template i have 2 elements, with 2 data_functions:
> <div n:data="totals"> - ???
> <div n:data="result"> - select * from db ... where ...
> But "totals" i will knew only after "result" makes request to DB
> So, i must to make "select * " in data_totals(), store data in
> self.var, and take it from self.var in data_result() function.
> This way is not perfect to me, coz if request to DB failed - it's very
> difficult to pass message about error into "request" function. May be
> not difficult but not clear. :(
You can store the db query in the context, retreive it from the
context in the "totals" data method and return data based on that,
though, I don't know how different this would be from just storing
data in the page object.
Solving this problem probably relies on more understanding of how
Nevow handles data, and when/where it's accessable.
> Another way is to use "fake" tag before "totals" and "request", like this:
> <n:invisible data="real_request">
> <div n:data="fetch_totals">
> <div n:data="fetch_data">
> Where data_real_request() doing "select *" and stores result (or error
> msg) in self.var
> It's not perfect too, coz we have program logic layer in template,
> and, again, it's not clear to programmer.
> Third way is to use 2 requests to DB - the worst way, coz requests are
> long in time.
> Is there another, perfect, clear way to produce such pages in nevow?
It might be helpful to post example code that's not working, you can
do non-blocking database queries with deferToThread or adbapi, if
you're using Twisted.
> Q2: About formless. How can i use my own check of form element's value?
> For example, i have "e-mail" field in form. And i wanna check char '@'
> existense in it.
> When my code falls into anotate.autocallable() function in
> FormImplementation class - i have only values of form elements.
> If e-mail value doesn't have "@"-char, how can i stop submitting and
> show error near "e-mail" field in form when RenderForm() called?
I wrote this a while ago:
...you might need to adapt it depending on your version of Nevow.
More information about the Twisted-web