首页 文章

从Rows对象Web2py中提取单个记录

提问于
浏览
1

我有以下数据库表:

db.define_table('game', Field('video', 'upload'),
                Field('pics', 'upload'),
                Field('answer'))

Field pics 和Field answers 每个都有6条记录 . video 字段只有1条记录 . 我必须显示 pics 记录和 answer 中的6条记录,然后用户必须选择正确的 answer 记录以匹配显示的 pics 记录 .

Controller

def test():
    rows = db().select(db.game.ALL, limitby=(0, 6), orderby='<random>')
    pic_row = rows[random.randint(0,4)]
    session.pic_id = pic_row.id
    return dict(rows=rows, pic_row=pic_row)

我在 View 中有这个 test.html

<h1>{{=pic_row.pics}}</h1>

{{for row in rows:}}
<h4>{{=B(LI(A(row.answer, _href=URL('show_test', args=row.id))))}}</h4>
{{pass}}

每次单击 answer 后,用户将被发送到 show_test.html ,并且从 show_test.html 开始,用户将再次返回 test.html . 我想从 pics 多次显示一条新记录(图像),但 random 将无效,因为它可能会重复同一条记录多次 . 如何在每次访问页面时显示来自 pics 的6条记录,而不重复任何记录?另一个相关问题是我想在用户第一次访问 test.html 时显示 video .

我想学习如何做到这一点,我感谢任何帮助 .

1 回答

  • 0
    def test():
        if not session.pic_id:
            session.pic_id = []
        rows = db(~db.game.id.belongs(session.pic_id)).select(db.game.ALL, limitby=(0, 6), orderby='<random>')
        pic_row = rows.first()
        session.pic_id.append(pic_row.id)
    
        if not session.already_visited:
            #Do something to show your video....
            session.already_visited = True     # Change it to true so it doesn't run this piece of code again
    
        return dict(rows=rows, pic_row=pic_row)
    

    该解决方案将保存会话中所有先前展出的图片,并且将仅随机检索图片一次 . 我不确定这是不是你真正想做的事情,因为在6张照片之后,没有任何其他东西可以显示给用户 .

相关问题