iterate over items in a database in controller

66 views
Skip to first unread message

John Martinetti

unread,
Jul 25, 2022, 1:12:10 PM7/25/22
to py4web
Hello -  

I have a controller function "services" that uses template "services.html"
I also have a table in the database called "services" that I can reference as "db.services".

I would like to dynamically build an unordered-list in the template using the list of services so I somehow need to iterate over the "services" table in the database & create a list of the field "svcname" & then pass that list to the template where I can dynamically display the list of services("svcname").   I hope I am explaining myself properly.

I have this for my table in models.py:
db.define_table(
    'services',
    Field('svcname'),
    Field('svcshort'),
    Field('svccat'),
    Field('svcdesc'),
    Field('svcrate'),
    Field('svcunit'),
    format='%(svcname)'
    )
I have this so far for the action in my controller, this is knowingly incomplete because I am stuck as to how to build the list I need from the database table "db.services":
@action("services")
@action.uses("services.html", db, auth, T)
def landing():
    page="Landing page! -  hopefully soon a dynamic list of services from the db"
    return dict(page=page)

And this is my template:
[[extend 'layout.html']]
<link rel="stylesheet" href="css/custom-styles.css>
<div class="services">
[[=page]]
</div>

I am hoping for some direction for how to iterate over the services database for the "svcname" field and then pass that list on to the template where I could then use a for-loop to spit out a styled-formatted list of the services.

Please let me know if I didn't explain it well.
Thanks -




Jim Steil

unread,
Jul 25, 2022, 2:43:52 PM7/25/22
to py4web
I'm not understanding what you're trying to get at.

From what I see, on the landing page, you want list all the services.  Then, when you click on a service, take you to a new page to show the details of that service.

Is that correct?

-Jim

Massimo DiPierro

unread,
Jul 25, 2022, 3:09:28 PM7/25/22
to Jim Steil, py4web
i think John is asking for something like:

@action("services")
@action.uses("services.html", db, auth, T)
def landing():
    page="Landing page! -  hopefully soon a dynamic list of services from the db"
    rows = db(db.services).select()
    return dict(page=page, services=rows)

And this is my template:
[[extend 'layout.html']]
<link rel="stylesheet" href="css/custom-styles.css>
<div class="services">
[[=page]]
[[for s in services:]]
<div>[[=s.svcname]]</div>
[[pass]]
</div>



--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/513da2a8-66eb-4f7a-9a9b-4c5616e3f763n%40googlegroups.com.

John Martinetti

unread,
Jul 25, 2022, 3:32:55 PM7/25/22
to py4web
Yes!  This is the part I was having a hard time with in my mind.  I'm new to py4web & not very good at Python but getting better.

The "rows" variable,  I didn't know how to assign the list of services because I didn't understand the db(db.services).select() method.  I will research that now to get a better understanding.   I think I maybe missed that in the DAL part of the docs...I will review.
I did see in the docs how to do the "[[for in services:]]"  loop, that is very handy & exactly what I wanted to emulate from what I saw in the docs.
I think the subject I used in this thread may have been misleading, sorry 'bout that.

Thank you both Massimo & Jim...let me try implementing that to confirm that it does what I think it should do.
Thank you again - I appreciate it.

Massimo

unread,
Jul 26, 2022, 7:48:39 AM7/26/22
to py4web
Feel free to ask more questions and point out what is lacking in the documentation.

John Martinetti

unread,
Jul 26, 2022, 2:33:39 PM7/26/22
to py4web
Thank you,  I think the docs are great, it's just a lot to digest & sometimes I can't figure out where to look for the functionality I've got in my head!
I will certainly reach-out again when I get stuck, thanks again.
Reply all
Reply to author
Forward
0 new messages