Django quiz-多个问题-数据库错误地保存正确答案,Pytest在302处失败

eimct9ow  于 2023-10-21  发布在  Go
关注(0)|答案(2)|浏览(83)

我正在努力构建一个多问题测验选项,但我不能让我的PostgreSQL保存正确的数据。数据库只保存第一个选项,而不关心它是否是正确的答案,以及是否有更多的正确答案。

“GET /multi-form/ HTTP/1.1”200 7693 <QueryDict:“csrfmiddlewaretoken”:'ALONiRKNa0QKQe6LQWacVz9vSQZPbFuyfdaLimGg9czBEy46hQAbAGyQatRLpI33'],'question':'“5”],“ans_count”:“5”],“answer_0”:'99'],'answer_1':'98'],'answer_2':'97'],'is_correct_2':请单击“on”],“answer_3”:“Qaz”],“answer_4”:请输入'Dacia'],'is_correct_4':打开“],”提交“:'Senden']}> 5 True ['99','is_correct':错误}]
99被选为唯一的答案,但它是不正确的,我选择了更多正确的选项,因为你可以在屏幕截图上发现。

forms.py(示例网站)

class AddMultipleForm(forms.ModelForm):
    ans_count = forms.IntegerField(min_value=1, label="Number of Answers Anzahl der Antworten")

    class Meta:
        model = MultiQuesModel
        fields = ["question", "language_name"]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        ans_count = self.initial.get("ans_count", 1)

        for i in range(ans_count):
            self.fields[f"answer_{i}"] = forms.CharField(max_length=200, required=True)
            self.fields[f"is_correct_{i}"] = forms.BooleanField(
                required=False, widget=forms.CheckboxInput(attrs={"class": "checkbox"})
            )

    def clean(self):
        cleaned_data = super().clean()
        ans_count = cleaned_data.get("ans_count")
        print(ans_count)
        answers = []

        for i in range(ans_count):
            answer_text = cleaned_data.get(f"answer_{i}")
            is_correct = cleaned_data.get(f"is_correct_{i}")

            if answer_text:
                answers.append({"text": answer_text, "is_correct": is_correct})

        if not answers:
            raise forms.ValidationError(
                "Please fill out at least one answer. Bitte füllen Sie mindestens eine Antwort aus."
            )

        cleaned_data["answers"] = answers
        return cleaned_data

Multiformular .html

{% extends 'dependencies.html' %}

{% block content %}

    <div class="jumbotron container row">
    <div class="col-md-6">
        <h1>Add Question</h1>
        <div class="card card-body">
            <form action="" method="POST">
                {% csrf_token %}
                {{ form.question.label_tag }} {{ form.question }}
                <br>
                {{ form.question.label_tag }} {{ form.language_name }}
                <label for="id_ans_count">Number of Answers Anzahl der Antworten:</label>
                {{ form.ans_count }}
                <br>
                <div id="answers-container">
                    {% for i in form.visible_fields %}
                        {% if 'answer_' in i.name or 'is_correct_' in i.name %}
                            {{ i.label_tag }} {{ i }}
                            <br>
                        {% endif %}
                    {% endfor %}
                </div>
                <input type="submit" name="Submit">
            </form>
        </div>
    </div>
</div>

    <script>
        // JavaScript, um dynamisch Antworten hinzuzufügen
        const ansCountInput = document.getElementById("id_ans_count");
        const answersContainer = document.getElementById("answers-container");

        ansCountInput.addEventListener("change", function () {
            const ansCount = parseInt(this.value);
            answersContainer.innerHTML = "";  // Leeren Sie das Container-Div

            for (let i = 0; i < ansCount; i++) {
                const answerField = document.createElement("div");
                answerField.innerHTML = `
                    <label for="id_answer_${i}">Answer ${i+1}:</label>
                    <input type="text" id="id_answer_${i}" name="answer_${i}" maxlength="200" required>
                    <label for="id_is_correct_${i}">Is Correct:</label>
                    <input type="checkbox" id="id_is_correct_${i}" name="is_correct_${i}">
                    <br>
                `;
                answersContainer.appendChild(answerField);
            }
        });
    </script>

  

{% endblock %}

models.py(示例网站)

class MultiQuesModel(models.Model):
    question = models.CharField(max_length=200, null=True)
    answers = models.ManyToManyField("Answer", through="QuestionAnswer")
    language_name = models.ForeignKey(Category, on_delete=models.CASCADE)  

    def __str__(self):
        return self.question

# Two fields possible
class Answer(models.Model):
    text = models.CharField(max_length=200)

    def __str__(self):
        return self.text

class QuestionAnswer(models.Model):
    question = models.ForeignKey(MultiQuesModel, on_delete=models.CASCADE)
    answer = models.ForeignKey(Answer, on_delete=models.CASCADE)
    is_correct = models.BooleanField(default=False)

views.py(示例网站)

class AddMultiQuestionView(View):
    def get(self, request):
        if request.user.is_staff:
            form = AddMultipleForm()
            context = {"form": form}
            return render(request, "multiformular.html", context)
        else:
            return redirect("index")

    def post(self, request):
        # if request.user.is_staff:
        form = AddMultipleForm(request.POST)
        print(request.POST)
        print(form.is_valid())
        print(form.errors)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()

            answers_data = form.cleaned_data["answers"]
            print(answers_data)
            for answer_data in answers_data:
                answer_text = answer_data["text"]
                is_correct = answer_data["is_correct"]

                answer, _ = Answer.objects.get_or_create(text=answer_text)
                instance.answers.add(answer, through_defaults={"is_correct": is_correct})

            return redirect("success_page")
        else:
            return render(request, "multiformular.html", {"form": form})
    # else:
    #     return redirect("index")
4smxwvx5

4smxwvx51#

现在我尝试为这个视图类AddMultiplayer View(View)添加Pytest:从这个问题。

conftest.py(示例网站)

import pytest
from django.contrib.auth.models import User

@pytest.fixture
def staff_user(db):
    user = User.objects.create_user(username='staffuser', password='staffpassword', is_staff=True)
    return user

@pytest.fixture
def regular_user(db):
    user = User.objects.create_user(username='testuser', password='testpassword')
    return user

test_views.py

import pytest
from django.test import Client
from django.urls import reverse
from ..models import MultiQuesModel, Answer
from django.contrib.auth.models import User
from ..views import AddMultiQuestionView

@pytest.mark.django_db
class TestAddMultiQuestionView:
    def test_view_requires_staff_login(self, client):
        url = reverse("multiform")
        response = client.get(url)
        assert response.status_code == 302  # Awaiting (Redirect)

    def test_view_displays_form_for_staff_user(self, client, staff_user):
        client.login(username='staffuser', password='staffpassword')
        url = reverse("multiform")
        response = client.get(url)
        assert response.status_code == 200
        assert "Number of Answers Anzahl der Antworten" in str(response.content)

    def test_add_multi_question(self, client, staff_user):
        client.login(username='staffuser', password='staffpassword')
        url = reverse("multiform")
        data = {
            "question": "Test question",
            "language_name": 2,  # Database
            "ans_count": 3,
            "answer_0": "Option 1",
            "is_correct_0": True,
            "answer_1": "Option 2",
            "is_correct_1": False,
            "answer_2": "Option 3",
            "is_correct_2": True,
        }
        print("Sending POST request to the view...")
        response = client.post(url, data)
        print("Received response from the view:", response)
        print(response.content)         # Added code- print out
        assert response.status_code == 302  # Awaiting (Redirect)
        assert MultiQuesModel.objects.count() == 1
        assert Answer.objects.count() == 2

代码

plugins: Faker-19.6.1, Flask-Dance-6.2.0, django-4.5.2
collected 3 items                                                                                                                                                                       

tests/test_views.py::TestAddMultiQuestionView::test_view_requires_staff_login PASSED                                                                                             [ 33%]
tests/test_views.py::TestAddMultiQuestionView::test_view_displays_form_for_staff_user PASSED                                                                                     [ 66%]
tests/test_views.py::TestAddMultiQuestionView::test_add_multi_question FAILED                                                                                                    [100%]

====================================================================================== FAILURES =======================================================================================
__________________________________________________________________ TestAddMultiQuestionView.test_add_multi_question ___________________________________________________________________

self = <Learntest.tests.test_views.TestAddMultiQuestionView object at 0x000001BB3960F9D0>, client = <django.test.client.Client object at 0x000001BB39A21950>
staff_user = <User: staffuser>

    def test_add_multi_question(self, client, staff_user):
        client.login(username='staffuser', password='staffpassword')
        url = reverse("multiform")
        data = {
            "question": "Test question",
            "language_name": 2,  # Passen Sie dies entsprechend Ihrer Datenbank an
            "ans_count": 3,
            "answer_0": "Option 1",
            "is_correct_0": True,
            "answer_1": "Option 2",
            "is_correct_1": False,
            "answer_2": "Option 3",
            "is_correct_2": True,
        }
        print("Sending POST request to the view...")
        response = client.post(url, data)
        print("Received response from the view:", response)
        print(response.content)         # Hinzugefügter Code: Printen Sie den Inhalt der Antwort
>       assert response.status_code == 302  # Erwartet eine Weiterleitung (Redirect)
E       assert 200 == 302
E        +  where 200 = <HttpResponse status_code=200, "text/html; charset=utf-8">.status_code

tests\test_views.py:40: AssertionError
-------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------- 
Sending POST request to the view...
<QueryDict: {'question': ['Test question'], 'language_name': ['2'], 'ans_count': ['3'], 'answer_0': ['Option 1'], 'is_correct_0': ['True'], 'answer_1': ['Option 2'], 'is_correct_1': ['False'], 'answer_2': ['Option 3'], 'is_correct_2': ['True']}>
3
False
<ul class="errorlist"><li>language_name<ul class="errorlist"><li>Select a valid choice. That choice is not one of the available choices.</li></ul></li></ul>
Received response from the view: <HttpResponse status_code=200, "text/html; charset=utf-8">
b'\n<html>\n    <head>\n        <title>\n            Quiz with Django\n        </title>\n        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/boo
tstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">\n    </head>\n    <body>\n        \n\n<style>\n  .greet{\n 
   font-size: 18px;\n    color: #fff;\n    margin-right: 20px;\n  }\n</style>\n\n<nav class="navbar navbar-expand-lg navbar-dark" style="background-color: rgba(34, 97, 21) ;">\n  <butt
on class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">\n    <span class="navbar-toggler-icon"></span>\n  </button>\n  <div class="collapse navbar-collapse" id="navbarNav">\n\n    <ul class="navbar-nav">\n\n          <li class="nav-item active">\n         
   <a class="nav-link" href="/index/">Main page</a>\n          </li>\n\n\n          <li class="nav-item active">\n          \n          </li>\n          <li class="nav-item active">\n 
           <a class="nav-link" href="/multi-form/">Multi question</a>\n          </li>\n\n          <li class="nav-item active">\n            <a class="nav-link" href="/single-form/">S
ingle question</a>\n          </li>\n          \n\n\n          <li class="nav-item">\n            <a class="nav-link" href="/accounts/login/">Login</a>\n          </li>\n\n          <l
i class="nav-item active">\n              <a class="nav-link" href="/register/">Register</a>\n          </li>\n\n          <li class="nav-item active">\n          \n          </li>\n  
        <li class="nav-item active">\n            <a class="nav-link" href="/question-form/">Text question</a>\n          </li>\n          \n\n          <li class="nav-item active">\n 
             <a class="nav-link" href="/multi-site/">Multi Quiz</a>\n          </li>\n\n          <li class="nav-item active">\n              <a class="nav-link" href="/notes/">Corpora
te Notes</a>\n          </li>\n\n          <li class="nav-item active">\n            \n          </li>\n              <a class="nav-link" href="/questionmanager/">Questionmanager</a>\n
          </li>\n          \n\n\n    </ul>\n  </div>\n\n  <span class="greet">Hello, staffuser</span>\n  <span ><a  class="greet" href="/logout/">Logout</a></span>\n  <span ><a  class=
"greet" href="/my-account/">My account</a></span>\n\n</nav>\n\n        \n\n    <div class="jumbotron container row">\n    <div class="col-md-6">\n        <h1>Add Question</h1>\n       
 <div class="card card-body">\n            <form action="" method="POST">\n                <input type="hidden" name="csrfmiddlewaretoken" value="lyBZOx7VgflkeI2SjzoFEgzHhlmMmM6V8JLCGg
M6YZt4Lrp7YOdR7DT4zA5FC84T">\n                <label for="id_question">Question:</label> <input type="text" name="question" value="Test question" maxlength="200" required id="id_questi
on">\n                <br>\n                <label for="id_question">Question:</label> <select name="language_name" required id="id_language_name">\n  <option value="">---------</optio
n>\n\n</select>\n                <label for="id_ans_count">Number of Answers Anzahl der Antworten:</label>\n                <input type="number" name="ans_count" value="3" min="1" required id="id_ans_count">\n                <br>\n                <div id="answers-container">\n                    \n                        \n                    \n                     
   \n                    \n                        \n                    \n                        \n                            <label for="id_answer_0">Answer 0:</label> <input type="text" name="answer_0" value="Option 1" maxlength="200" required id="id_answer_0">\n                            <br>\n                        \n                    \n                  
      \n                            <label for="id_is_correct_0">Is correct 0:</label> <input type="checkbox" name="is_correct_0" class="checkbox" id="id_is_correct_0" checked>\n      
                      <br>\n                        \n                    \n                        \n                            <label for="id_answer_1">Answer 1:</label> <input type="text" name="answer_1" value="Option 2" maxlength="200" required id="id_answer_1">\n                            <br>\n                        \n                    \n                 
       \n                            <label for="id_is_correct_1">Is correct 1:</label> <input type="checkbox" name="is_correct_1" class="checkbox" id="id_is_correct_1">\n             
               <br>\n                        \n                    \n                        \n                            <label for="id_answer_2">Answer 2:</label> <input type="text" name="answer_2" value="Option 3" maxlength="200" required id="id_answer_2">\n                            <br>\n                        \n                    \n                        
\n                            <label for="id_is_correct_2">Is correct 2:</label> <input type="checkbox" name="is_correct_2" class="checkbox" id="id_is_correct_2" checked>\n            
                <br>\n                        \n                    \n                </div>\n                <input type="submit" name="Submit">\n            </form>\n        </div>\n
    </div>\n</div>\n\n    <script>\n        // JavaScript, um dynamisch Antworten hinzuzuf\xc3\xbcgen\n        const ansCountInput = document.getElementById("id_ans_count");\n        c
..\..\..\.venv\Lib\site-packages\_pytest\config\__init__.py:1373
  C:\Users\cairn\Documents\Kodowanie\.venv\Lib\site-packages\_pytest\config\__init__.py:1373: PytestConfigWarning: Unknown config option: python_paths

    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=============================================================================== short test summary info =============================================================================== 
FAILED tests/test_views.py::TestAddMultiQuestionView::test_add_multi_question - assert 200 == 302
======================================================================= 1 failed, 2 passed, 1 warning in 4.16s

如果我手动测试它,它会工作,它会被写入数据库,我会被重定向:

[10/Oct/2023 22:38:25] "GET /multi-form/ HTTP/1.1" 200 7701
<QueryDict: {'csrfmiddlewaretoken': ['imjj4MI6Y4IRjX4nzVCGhfadPnwLXPRfoCMfDuiZxdV6QS24BG6VzmrQ0domC0x0'], 'question': ['Pytest?'], 'language_name': ['4'], 'ans_count': ['5'], 'answer_0': ['Snake'], 'is_correct_0': ['on'], 'answer_1': ['Cow'], 'answer_2': ['Human'], 'answer_3': ['Kangaroo'], 'answer_4': ['Dacia'], 'is_correct_4': ['on'], 'Submit': ['Senden']}>       
5
True

<QueryDict: {'question': ['Test question'], 'language_name': ['2'], 'ans_count': ['3'], 'answer_0': ['Option 1'], 'is_correct_0': ['True'], 'answer_1': ['Option 2'], 'is_correct_1': ['False'], 'answer_2': ['Option 3'], 'is_correct_2': ['True']}>
3
False
<ul class="errorlist"><li>language_name<ul class="errorlist"><li>Select a valid choice. That choice is not one of the available choices.</li></ul></li></ul>
b'\n<html>\n    <head>\n        <title>\n            Quiz with Django\n        </title>\n        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/boo
tstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">\n    </head>\n    <body>\n        \n\n<style>\n  .greet{\n 
   font-size: 18px;\n    color: #fff;\n    margin-right: 20px;\n  }\n</style>\n\n<nav class="navbar navbar-expand-lg navbar-dark" style="background-color: rgba(34, 97, 21) ;">\n  <butt
on class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">\n    <span class="navbar-toggler-icon"></span>\n  </button>\n  <div class="collapse navbar-collapse" id="navbarNav">\n\n    <ul class="navbar-nav">\n\n          <li class="nav-item active">\n         
   <a class="nav-link" href="/index/">Main page</a>\n          </li>\n\n\n          <li class="nav-item active">\n          \n          </li>\n          <li class="nav-item active">\n 
           <a class="nav-link" href="/multi-form/">Multi question</a>\n          </li>\n\n          <li class="nav-item active">\n            <a class="nav-link" href="/single-form/">S
ingle question</a>\n          </li>\n          \n\n\n          <li class="nav-item">\n            <a class="nav-link" href="/accounts/login/">Login</a>\n          </li>\n\n          <l
i class="nav-item active">\n              <a class="nav-link" href="/register/">Register</a>\n          </li>\n\n          <li class="nav-item active">\n          \n          </li>\n  
        <li class="nav-item active">\n            <a class="nav-link" href="/question-form/">Text question</a>\n          </li>\n          \n\n          <li class="nav-item active">\n 
             <a class="nav-link" href="/multi-site/">Multi Quiz</a>\n          </li>\n\n          <li class="nav-item active">\n              <a class="nav-link" href="/notes/">Corpora
te Notes</a>\n          </li>\n\n          <li class="nav-item active">\n            \n          </li>\n              <a class="nav-link" href="/questionmanager/">Questionmanager</a>\n
          </li>\n          \n\n\n    </ul>\n  </div>\n\n  <span class="greet">Hello, staffuser</span>\n  <span ><a  class="greet" href="/logout/">Logout</a></span>\n  <span ><a  class=
"greet" href="/my-account/">My account</a></span>\n\n</nav>\n\n        \n\n    <div class="jumbotron container row">\n    <div class="col-md-6">\n        <h1>Add Question</h1>\n       
 <div class="card card-body">\n            <form action="" method="POST">\n                <input type="hidden" name="csrfmiddlewaretoken" value="HpTXXXBcTZTNGJR51VtvjmOHr8wywRT0RRX3o9
RLCV8k5o9p8yJknJ8Lx8s5BMR8">\n                <label for="id_question">Question:</label> <input type="text" name="question" value="Test question" maxlength="200" required id="id_questi
on">\n                <br>\n                <label for="id_question">Question:</label> <select name="language_name" required id="id_language_name">\n  <option value="">---------</optio
n>\n\n</select>\n                <label for="id_ans_count">Number of Answers Anzahl der Antworten:</label>\n                <input type="number" name="ans_count" value="3" min="1" required id="id_ans_count">\n                <br>\n                <div id="answers-container">\n                    \n                        \n                    \n                     
   \n                    \n                        \n                    \n                        \n                            <label for="id_answer_0">Answer 0:</label> <input type="text" name="answer_0" value="Option 1" maxlength="200" required id="id_answer_0">\n                            <br>\n                        \n                    \n                  
      \n                            <label for="id_is_correct_0">Is correct 0:</label> <input type="checkbox" name="is_correct_0" class="checkbox" id="id_is_correct_0" checked>\n      
                      <br>\n                        \n                    \n                        \n                            <label for="id_answer_1">Answer 1:</label> <input type="text" name="answer_1" value="Option 2" maxlength="200" required id="id_answer_1">\n                            <br>\n                        \n                    \n                 
       \n                            <label for="id_is_correct_1">Is correct 1:</label> <input type="checkbox" name="is_correct_1" class="checkbox" id="id_is_correct_1">\n             
               <br>\n                        \n                    \n                        \n                            <label for="id_answer_2">Answer 2:</label> <input type="text" name="answer_2" value="Option 3" maxlength="200" required id="id_answer_2">\n                            <br>\n                        \n                    \n                        
\n                            <label for="id_is_correct_2">Is correct 2:</label> <input type="checkbox" name="is_correct_2" class="checkbox" id="id_is_correct_2" checked>\n            
                <br>\n                        \n                    \n                </div>\n                <input type="submit" name="Submit">\n            </form>\n        </div>\n
    </div>\n</div>\n\n    <script>\n        // JavaScript, um dynamisch Antworten hinzuzuf\xc3\xbcgen\n        const ansCountInput = document.getElementById("id_ans_count");\n        c
onst answersContainer = document.getElementById("answers-container");\n\n        ansCountInput.addEventListener("change", function () {\n            const ansCount = parseInt(this.valu
wer_${i}" name="answer_${i}" maxlength="200" required>\n                    <label for="id_is_correct_${i}">Is Correct:</label>\n                    <input type="checkbox" id="id_is_correct_${i}" name="is_correct_${i}">\n                    <br>\n                `;\n                answersContainer.appendChild(answerField);\n            }\n        });\n    </script>\n\n    <!-- Uhranzeige mit JavaScript -->\n    <div class="clock" id="clock"></div>\n\n    <script>\n        // JavaScript, um die Uhrzeit anzuzeigen\n        function updateClock() {\n            const clockElement = document.getElementById("clock");\n            const now = new Date();\n            const hours = now.getHours().toString().padStart(2, "0");\n            const minutes = now.getMinutes().toString().padStart(2, "0");\n            const seconds = now.getSeconds().toString().padStart(2, "0");\n            const date = now.toDateStrin[10/Oct/2023 22:38:57] "POST /multi-form/ HTTP/1.1" 302 0
[10/Oct/2023 22:38:57] "GET /success-page/ HTTP/1.1" 200 1419
[10/Oct/2023 22:38:59] "GET /questionmanager/ HTTP/1.1" 200 2818

[{'text': 'Snake', 'is_correct': True}, {'text': 'Cow', 'is_correct': False}, {'text': 'Human', 'is_correct': False}, {'text': 'Kangaroo', 'is_correct': False}, {'text': 'Dacia', 'is_correct': True}]
Preparing to redirect...
[10/Oct/2023 22:38:57] "POST /multi-form/ HTTP/1.1" 302 0
[10/Oct/2023 22:38:57] "GET /success-page/ HTTP/1.1" 200 1419
[10/Oct/2023 22:38:59] "GET /questionmanager/ HTTP/1.1" 200 2818
zte4gxcn

zte4gxcn2#

主要问题可能是ans_count,除非指定,否则它总是使用1,因此使用 one item。这意味着即使您发送了五个项目,清理仍然只会检索第一个。例如,我们可以将其设置为5,以获得前五个项目:

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    ans_count = self.initial.get('ans_count', 5)  # 🖘 first five

    for i in range(ans_count):
        self.fields[f'answer_{i}'] = forms.CharField(
            max_length=200, required=True
        )
        self.fields[f'is_correct_{i}'] = forms.BooleanField(
            required=False,
            widget=forms.CheckboxInput(attrs={'class': 'checkbox'}),
        )

当然,如果用户输入更多的项,这些项将无法解析。
您可以从数据中确定这些,使用:

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    ans_count = self.initial.get('ans_count', 1)
    if self.data is not None:
        try:
            ans_count = int(self.data.get('ans_count', ans_count))
        except ValueError:
            pass

    for i in range(ans_count):
        self.fields[f'answer_{i}'] = forms.CharField(
            max_length=200, required=True
        )
        self.fields[f'is_correct_{i}'] = forms.BooleanField(
            required=False,
            widget=forms.CheckboxInput(attrs={'class': 'checkbox'}),
        )

话虽如此,您尝试做的是已经由FormSet [Django-doc]完成的大部分工作,因此您可能想看看它是否提供了一些支持,特别是因为验证多个小表单至少和单个表单一样麻烦。

相关问题