效果图
#获取题目
@csrf_exempt
def getquestion(request):
questionid=random.randint(1,28667)
questioninfo=idiom.objects.get(id=questionid)
#缺省值那个置为空
if questioninfo.idiom_word1==questioninfo.idiom_lack:
questioninfo.idiom_word1=""
if questioninfo.idiom_word2==questioninfo.idiom_lack:
questioninfo.idiom_word2=""
if questioninfo.idiom_word3==questioninfo.idiom_lack:
questioninfo.idiom_word3=""
if questioninfo.idiom_word4==questioninfo.idiom_lack:
questioninfo.idiom_word4=""
#选项随机取值3个 再加答案选项
optionrand=random.sample(questioninfo.idiom_option,3)
selnum=random.randint(1,4)
if selnum==1:
option1=questioninfo.idiom_lack
option2=optionrand[0]
option3=optionrand[1]
option4=optionrand[2]
elif selnum==2:
option1=optionrand[0]
option2=questioninfo.idiom_lack
option3=optionrand[1]
option4=optionrand[2]
elif selnum==3:
option1=optionrand[0]
option2=optionrand[1]
option3=questioninfo.idiom_lack
option4=optionrand[2]
else:
option1=optionrand[0]
option2=optionrand[1]
option3=optionrand[2]
option4=questioninfo.idiom_lack
resdata={}
resdata["questionid"]=questionid
resdata["word1"]=questioninfo.idiom_word1
resdata["word2"]=questioninfo.idiom_word2
resdata["word3"]=questioninfo.idiom_word3
resdata["word4"]=questioninfo.idiom_word4
resdata["option1"]=option1
resdata["option2"]=option2
resdata["option3"]=option3
resdata["option4"]=option4
resdata=json.dumps(resdata)
return HttpResponse(resdata,content_type="application/json")
新手学py