Как создать викторину с ограниченным количеством вопросов в Django - PullRequest
0 голосов
/ 30 октября 2019

Я хочу создать викторину с заранее заданным количеством вопросов, и эти вопросы будут отображаться наугад. В настоящее время он генерируется только по одному. Как сделать?

views.py

def quiz(request):
    qtd = request.GET.get('qtd',None)
    questao = Questao.objects.annotate(resp_count=models.Count(models.Case(models.When(resposta__usuario=request.user, then=1),output_field=models.IntegerField()))).filter(resp_count=0,tipoQuestao=1,statusQuestao=1).order_by("?").first()
    q = request.POST.get('idQuestao',None)
    respostas = Resposta.objects.filter(usuario=request.user,idQuestao=questao)
    respostaform = RespostaForm(request.GET,request.POST or None)
    if request.method == 'POST':
        if respostaform.is_valid():
            resp = respostaform.save(commit=False)
            resp.resposta = request.POST.get('resposta','')
            resp.usuario = request.user
            resp.idQuestao_id = q
            resp.save()
        return HttpResponseRedirect(request.path_info)
    else:
        respostaform = RespostaForm()

quiz.html

{% if not respostas %}
    <h3>Questão {{questao.idQuestao}}</h3>
    {% if questao.textoQuestao %}
        <p>{{questao.textoQuestao}}</p>
    {% endif %}
    {% if questao.perguntaQuestao %}
        <p>{{questao.perguntaQuestao}}</p>
    {% endif %}
    <form id="respostaform" name="respostaform" action="" method="POST">
         {% csrf_token %}
        {{respostaform.resposta}}
        <input class="btn btn-primary btn-block" type="submit" value="Responder">
    </form>  
{% endif %}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...