Hi Sai,

Can you try this:

import  random

## first option to select question
s = number of lines of your table
i = random.randint(0,s)

## second option to select question - best: no broke if delete any record
on table
s = db(db.ins_ques.id > 0).select(db.ins_ques.id)
i = random.sample(s,1)

## how to query your question
question=db(db.ins_ques.id == i).select(db.ins_ques.ALL).first()

[...]


Fabiano.

2015-07-28 5:51 GMT-03:00 Sai Harsh Tondomker <saiharsh....@iiits.in>:

> I have trying to display random question for every login (Means client get
> different questions for every login).
> Here I gave my db.py and def doing paper. Please help me to solve the
> problem.
>
>
> db.define_table('ins_ques',
> Field('qnum','integer',notnull=True,readable=False,writable=False,label='Question
> Number'),
> Field('qupload','upload',label='Upload Image'),
> Field('question','text',notnull=True,label='Question'),
> Field('op1','string',notnull=True,label='Option A'),
> Field('op2','string',notnull=True,label='Option B'),
> Field('op3','string',notnull=True,label='Option C'),
> Field('op4','string',notnull=True,label='Option D'),
> Field('cor_ans',requires=IS_IN_SET(['A','B','C','D'],multiple=True),label='Correct
> Answer',widget=SQLFORM.widgets.checkboxes.widget)
> )
> db.define_table('ans_ques',
> Field('qdate','date',readable=False,writable=False,label='Date of Paper'),
> Field('qnum','integer',readable=False,writable=False),
> Field('studentid','integer',readable=False,writable=False),
>
> Field('answer','string',requires=IS_IN_SET(['A','B','C','D'],multiple=True),widget=SQLFORM.widgets.checkboxes.widget)
> )
> db.define_table('marks',
> Field('studentid',db.auth_user,requires=IS_IN_DB(db((db.auth_user.id==db.auth_membership.user_id)&(db.auth_membership.group_id==
>    db.auth_group.id)&(db.auth_group.role=='students')),'auth_user.id
> ','auth_user.first_name'),readable=False,writable=False),
> Field('marks','integer'))
>
>
>
> def doing_paper():
>     pid=auth.user_id
>     q_num=int(request.vars.q_num)
>     question=db((q_num==db.ins_ques.qnum)).select(db.ins_ques.ALL)
>     val=db(db.ins_ques).select(db.ins_ques.ALL)
>     rmax=0
>     for f in val:
>           rmax+=1
>     if (q_num <= 0):
> response.flash='This is the first question!!!!'
> q_num=1
> redirect(URL(r=request,f='doing_paper?q_num=%s') % (q_num) )
>     elif (len(question) > 0) :
>     for i in question:
>             if i:
>                 question=i
>                 break
>             else:
>                 q_num=q_num-1
>                 redirect(URL(r=request,f='preprocess?q_num=%s') % (q_num))
>     else:
> q_num=q_num-1
> redirect(URL(r=request,f='preprocess?q_num=%s') % (q_num))
>     if question:
> form=SQLFORM.factory(db.ans_ques)
>     form.vars.qnum=q_num
>     form.vars.studentid=pid
>
> answered=db((db.ans_ques.answer!='||')&(db.ans_ques.studentid==pid)).select(db.ans_ques.qnum)
>     n=[]
>     for k in answered:
>         n.append(k['qnum'])
>
> ans_yet=db((db.ans_ques.studentid==pid)&(db.ans_ques.qnum==q_num)).select(db.ans_ques.answer)
>     if(ans_yet and ans_yet[0]['answer']!='||'):
>         form.vars.answer=ans_yet[0]['answer']
>     if form.accepts(request.vars,session):
> if
> db((db.ans_ques.qnum==q_num)&(db.ans_ques.studentid==pid)).select(db.ans_ques.answer):
>
> db((db.ans_ques.qnum==q_num)&(db.ans_ques.studentid==pid)).update(answer=form.vars.answer)
> else:
> db.ans_ques.insert(answer=form.vars.answer,qnum=q_num,studentid=pid)
> q_num=q_num+1
> redirect(URL(r=request,f='doing_paper?q_num=%s') % (q_num))
>     return locals()
>
> Regards
> Sai Harsh
>
> --
> 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 the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to web2py+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to