문제

I have code in web2py. I want to print out a single row record but all I get is all the rows. Here is the code:

def view_post:
    form = SQLFORM(db.post.name)
    for rows in db.select(db.post.name)
    return dict()

view post:

{{extend 'layout.html'}}

{{=form}}

what I get is:

post.name 1. name1 2. name2 3. name3 etc

I want a way to pick only e.g: post.name 2.name2

도움이 되었습니까?

해결책

If you want to display one record of the result of a query, you could use SQLFOM.grid :

    def view_post:
        form = SQLFORM.grid( query = db.post.id==10 )
        return dict( form=form )

You can replace 10 for anything you want, or change the query. If you want to check out a more complex example, check the Web2py slices http://www.web2pyslices.com/slice/show/1471/sqlformgrid

다른 팁

The code you have posted should raise an exception. First, you cannot call the .select() method on a db object. Second, you return an empty dictionary to the view, but then refer to the form object in the view (which won't exist in the view's environment).

If you want to display a read-only form of a given record, you can do something like:

def view_post:
    post = SQLFORM(db.post, record=request.args(0), readonly=True)
    return dict(post=post)

And in the view:

{{=post}}

The above code assumes you pass in the record ID as the first part of the URL after the controller and function (i.e., the first URL arg, which is stored in request.args(0)). You can add some error checking to first make sure the record exists.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top