0

I'm new to Python and I'm trying to make a simple bulletin board system app using web2py. I am trying to add a post into a certain board and I linked the post and board by including the following field in my post table: Field('board_id', db.board). When I try to create a post inside a particular board it gives me an error: "OperationalError: no such column: board.id". My code for create_posts:

    def add_post():
        board = db.board(request.args(0))
        form = SQLFORM(db.post)
        db.pst.board_id.default = db.board.id
        if form.process().accepted:
            session.flash = T('The data was inserted')
            redirect(URL('default', 'index'))
        return dict(form=form, board=board)

When I try to do {{=board}} on the page that shows the posts in a certain board, I get Row {'name': 'hi', 'id': 1L, 'pst': Set (pst.board_id = 1), 'description': 'hi'} so I know it's there in the database. But when I do the same thing for the "add post" form page, it says "board: None". I'm extremely confused, please point me in the right direction!

  • `db.board(request.args(0))` returns the record from `db.board` whose `id` is equal to `request.args(0))` or `None` if there is no record with that `id`. So, either there is no value for `request.args(0)`, or its value does not match any of your record IDs. Presumably you are generating links to `add_post` that include a record ID as the first URL arg. How are you generating those links? – Anthony Oct 29 '15 at 11:13

1 Answers1

0

There appear to be several problems with your function. First, you are assigning the default value of the board_id field to be a Field object (i.e., db.board.id) rather than an actual id value (e.g., board.id). Second, any default values should be assigned before creating the SQLFORM.

Finally, you pass db.post to SQLFORM, but in the next line, the post table appears to be called db.pst -- presumably these are not two separate tables and one is just a typo.

Regarding the issue of {{=board}} displaying None, that indicates that board = db.board(request.args(0)) is not retrieving a record, which would be due to request.args(0) itself being None or being a value that does not match any record id in db.board. You should check how you are generating the links that lead to add_post and confirm that there is a valid db.board id in the first URL arg. In any case, it might be a good idea to detect when there is no valid board record and either redirect or display an error message.

So, your function should look something like this:

def add_post():
    board = db.board(request.args(0)) or redirect(URL('default', 'index'))
    db.pst.board_id.default = board.id
    form = SQLFORM(db.pst)
    if form.process(next=URL('default', 'index'),
                    message_onsuccess=T('The data was inserted'))
    return dict(form=form, board=board)

Note, if your are confident that links to add_post will include valid board IDs, then you can eliminate the first line altogether, as there is no reason to retrieve a record based on its ID if the only field you need from it is the ID (which you already have). Instead, the second line could be:

    db.pst.board_id.default = request.args(0) or redirect(URL('default', 'index'))
Anthony
  • 25,466
  • 3
  • 28
  • 57
  • thank you SO much!!!! this line worked when i replaced the first two lines with it: db.pst.board_id.default = request.args(0) or redirect(URL('default', 'index')) – user3883570 Oct 29 '15 at 15:30