如何在html(Django)上运行一次for循环

ie3xauqp  于 2023-01-03  发布在  Go
关注(0)|答案(1)|浏览(158)

enter image description here当我在问题中运行{% for q时,如何运行一次for循环|slice:':1'%}数据库中的其他问题未执行。我希望每页仅显示一个问题,单击"下一步"后将加载下一个问题。我不知道如何解决此问题。请查看图片,了解如何在一页上将两个问题而不是一个问题加载到数据库中。
我希望有人有办法

<form method="POST" action="">
    {% for q in questions%}
        {% csrf_token %}
        {% if q.kategorie == category and q.flaag == True and forloop.first  %}
            <br/>
            <div class="flex-containe">
                <div class="container1">
                    <div class="position_startButton"><button  type="submit" name="next_question"  value="{{q.question}}"   class="neuefragenladen_button">Nächste Frage!</button></div>
                    <div class="game_options">
                        <span>
                            <input type="subit" id="option1" name="option1"  class="radio" value="option1"/>
                            <label for="option1" class="option" id="option1_label">{{q.op1}}</label>
                        </span>
                        <span>
                            <input type="subit" id="option2" name="option2" class="radio" value="option2"/>
                            <label for="option2" class="option" id="option2_label">{{q.op2}}</label>
                        </span>
                        <div class="game_question">
                            <h1 id="display_question">{{q.question}}</h1>
                        </div>
                        <span>
                            <input type="subit" id="option3" name="option3" class="radio" value="option3"/>
                            <label for="option3" class="option" id="option3_label">{{q.op3}}</label>
                        </span>
                        <span>
                            <input type="subit" id="option4" name="option4" class="radio" value="option4"/>
                            <label for="option4" class="option" id="option4_label">{{q.op4}}</label>
                        </span>
                    </div>
                </div>

            </div>

        <footer class="bottom-area">
            <a class="information" href="https://www.google.at/?hl=de.">Impressum |</a>
            <a class="information" href="https://www.google.at/?hl=de.">Datenschutz |</a>
            <a class="information" href="https://www.google.at/?hl=de.">Support</a>
        </footer>
        .....
views.py

def quiz(request):
    global globalCategory
    global globalQuestions
    global empty
    global first
    global probe
    global globalprobe


    count_of_questions = len(QuesModel.objects.filter(kategorie__exact=globalCategory))  -1


    if request.method == 'POST' and 'next_question'  in request.POST and count_of_questions > len(empty):

        previousQuestion = request.POST['next_question']

        globalQuestions = globalQuestions.exclude(question__exact=previousQuestion)

        empty.append(1)


        context = {
            'questions': globalQuestions,
            'category': globalCategory,
        }
        return render(request, 'quiz.html', context)

    elif first == False :
        globalCategory = request.POST.get("Kategorie")
        first = True
        questions = QuesModel.objects.all()
        globalQuestions = questions
        context = {
            'questions': questions,
            'category': request.POST.get("Kategorie"),

        }

        return render(request, 'quiz.html', context)

    elif request.POST.get("Start") and first == True:
        first = False
        return render(request, 'Example_dashboard.html')

    else:
        print(first)
        empty.clear()
        first = False
        return render(request, 'result.html')
models.py
class QuesModel(models.Model):
    Kategorie = (
        ("math", "mathn"),
        ("Digital Skills", "Digital Skills"),

    )

    kategorie = models.CharField(max_length=400, null=True, choices=Kategorie)
    question = models.CharField(max_length=200, null=True,)
    op1 = models.CharField(max_length=200, null=True)
    op2 = models.CharField(max_length=200, null=True)
    op3 = models.CharField(max_length=200, null=True)
    op4 = models.CharField(max_length=200, null=True)
    ans = models.CharField(max_length=200, null=True)
    flag = models.BooleanField('Aprroved', default=False)
    def __str__(self):
        return self.question

当我在questions中运行{% for q}时,如何运行一次for循环|slice:':1'%}数据库中的其他问题未执行。我希望每页仅显示一个问题,单击"下一步"后将加载下一个问题。我不知道如何解决此问题。请查看图片,了解如何在一页上将两个问题而不是一个问题加载到数据库中。
我希望有人有办法

wrrgggsh

wrrgggsh1#

如果你想每页显示1个或更多的对象,你需要学习pagination .或者你可以循环所有的对象,然后用javascript和css在前端控制它们...更多细节请搜索“django中的分页”.你可以在youtube上找到成千上万的教程.

相关问题