Sending data from view form to controller

139 views
Skip to first unread message

Wonton

unread,
Oct 17, 2013, 11:22:40 PM10/17/13
to web...@googlegroups.com
Hello,

I've been working with web2py controllers to connect with my database and I haven't use views so far, so I don't know if what I'm trying to do is very wrong. In that case, please, tell me ;-).

Suppose I have this:

controllers/mycontroller.py:

def show_matches():
    matches
= db(db.matches.id > 0).select()
   
return dict(matches=matches)

views/show_matches.html:

{{extend 'layout.html'}}
<h1>MATCHES</h1>
<table>
<th>
    <th>MATCH</
th>
   
<th>TEAM1</th>
    <th>TEAM2</
th>
</th>
{{for match in matches:}}
<tr>
    <td>{{=match['team1']}} vs. {{=match['team2']}}</
td>
   
<td>
       
<forms>
           
<select style="width:80px;">
             
<option>0</option>
              <option>1</
option>
             
<option>2</option>
              <option>+2</
option>
           
</select>
        </
form>
   
</td>
    <td>
        <forms>
            <select style="width:80px;">
              <option>0</
option>
             
<option>1</option>
              <option>2</
option>
             
<option>+2</option>
            </
select>
       
</form>
   </
td>
</tr>
{{pass}}
</
table>


I would like to include a button (with 'a' and 'onclick' I guess) at the bottom of the page so when the user clicks in it, all data selected in the forms are sent to mycontroller.py (to store_results_in_ddbb function).

How could I read all this data and send it to a controller? Should I have only one <form> and all <select> components pointing to it through a 'form' tag? Should I have all select components with an 'id' tag and read the data through this tag?

I'm a newbie with HTML (web frontend in general) so I hope I am not saying something very stupid ;-).

Any idea will be very appreciated.

Thank you very much and kind regards!

Tim Richardson

unread,
Oct 18, 2013, 2:00:18 AM10/18/13
to
You are doing this the really hard way, I think.
If you read the book a bit more you will discover web2py's built-in forms.
Probably you should use SQLFORM.
This means you do all the coding in the controller. Behind the scenes, SQLFORM will create a variable which contains the HTML to display the form, and it also includes the logic to update the database table. 

While you are developing, you can rely on the "developer" or generic views, which means you don't even need to worry about making a view until you are ready.

In other words, you can do all of this in one line (in the controller)

form = SQLFORM(db.matches)

(assuming you have setup up your table definition well). 


Or you should read about SQLFORM.grid, which may be more what you want. It starts by displaying all the records in a table, and then lets the user choose which rows to edit. It then automatically generates an SQLFORM for that row. Still only one line :)

Wonton

unread,
Oct 18, 2013, 8:28:47 PM10/18/13
to web...@googlegroups.com
Thank you very much Tim!!

I didn't even consider this posibility. It seems that it's what I was looking for. I will try it.

Kind regards!


On Friday, October 18, 2013 3:56:43 AM UTC+2, Tim Richardson wrote:
You are doing this the really hard way, I think.
If you read the book a bit more you will discover web2py's built-in forms.
Probably you should use SQLFORM.
This means you do all the coding in the controller. Behind the scenes, SQLFORM will create a variable which contains the HTML to display the form, and it also includes the logic to update the database table. 

While you are developing, you can rely on the "developer" or generic views, which means you don't even need to worry about making a view until you are ready.

In other words, you can do all of this in one line (in the controller)

form = SQLFORM(db.matches)

(assuming you have setup up your table definition well). 


Or you should read about SQLFORM.grid, which may be more what you want. It starts by displaying all the records in a table, and then lets the user choose which rows to edit. It then automatically generates an SQLFORM for that row. Still only one line :)






On Friday, 18 October 2013 10:22:40 UTC+11, Wonton wrote:

Wonton

unread,
Oct 19, 2013, 4:03:23 PM10/19/13
to web...@googlegroups.com
Hello again!

After some reading about SQLForm, obviously that's what I need. But since there are some different options I would like to choose the correct one. For my case, should I use a SQLForm.grid or a SQLForm.factory()? And in the case of using a grid, is it posible to have a select list (something like a drop-down list) for each row or I have to use the "Update" button for each one?

Kind regards!

Massimo Di Pierro

unread,
Oct 19, 2013, 10:26:52 PM10/19/13
to web...@googlegroups.com
You use SQLFORM.factory if you want a form not connected to the database. You use SQLFORM.grid if you want to show database records in tabular form.

Tim Richardson

unread,
Oct 19, 2013, 10:40:45 PM10/19/13
to web...@googlegroups.com

A grid means a button to go to an edit form.  An inline edit solution is possible but more advanced,  not out of the box. 

--
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/1W73GQvGVEA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages