这是在同一个视图中处理多个表单集的好方法吗?

时间:2019-05-21 07:04:00

标签: python django formset

首先我使用的是Python 3.7.3和Django 2.2

我在同一视图中处理多个表单集时遇到问题。在我看来,我正在for循环中创建表单集(在下面进行检查),然后将它们添加到列表中。在我的模板中,对该列表执行一个for循环,并在模板中显示表单集。我认为我所做的工作很好,但是当表单集无效时会发生错误(因为我也进行了一些验证)。当某个表单集无效(因为不接受值)时,其他表单集也将使用这些值进行初始化。为什么?

models.py

class Set(models.Model):
    timeSet     = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
    scoreTeam1  = models.IntegerField(null=True)
    scoreTeam2  = models.IntegerField(null=True)
    match       = models.ForeignKey(Match, default=None, on_delete=models.CASCADE)

class Match(models.Model):
    isFinished  = models.BooleanField(default=False)
    team1Win    = models.BooleanField(default=False)
    team2Win    = models.BooleanField(default=False)
    phase       = models.ForeignKey(Phase, default=None, on_delete=models.CASCADE)
    teams       = models.ManyToManyField(Team, default=None, blank=True)

tournament_manage_phase_matches.html

{% for match in matches %}
                    {% if match.teams.first.pool == pool %}
                        <div class="col-lg-12">
                            {% if match.isFinished == False %}
                                <div class="btn-group dropright">
                                    <button type="button" class="btn btn-secondary">
                                        Field number : {{ pool.field.numField }} | {{ match.teams.first.name }} VS {{ match.teams.last.name }}
                                    </button>
                                    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                        <span class="sr-only">Toggle Dropright</span>
                                    </button>
                                    <div class="dropdown-menu">
                                        {% for formset_set in list_formset_set %}
                                            {% if formset_set.match == match %}
                                                <form class="px-4 py-3" method="post">
                                                {% csrf_token %}
                                                    {{ formset_set|crispy }}
                                                    {{ formset_set.management_form }}
                                                    <button type="submit" class="btn btn-primary" value="{{ match.pk }}" name="match_pk">Save score</button>
                                                </form>
                                            {% endif %}
                                        {% endfor %}
                                    </div>
                                </div>
                            {% else %}
                                {% if match.team1Win == False and match.team2Win == False %}
                                    <label>{{ match.teams.first.name }} VS {{ match.teams.last.name }} : Match nul</label>
                                {% else %}
                                    {% if match.team1Win == True and match.team2Win == False %}
                                        <label>{{ match.teams.first.name }} VS {{ match.teams.last.name }} : Victory {{ match.teams.first.name }}</label>
                                    {% endif %}
                                    {% if match.team1Win == False and match.team2Win == True %}
                                        <label>{{ match.teams.first.name }} VS {{ match.teams.last.name }} : Victory {{ match.teams.last.name }}</label>
                                    {% endif %}
                                {% endif %}
                            {% endif %}
                        </div>
                    {% endif %}
                {% endfor %}

总而言之,当我提交一个表单集时,如果它无效,所有其他表单集都将变得像我刚提交的表单集,我不明白为什么。 如果您发现我的操作有问题,请也告诉我:)

编辑:
views.py

...
#creation of forms
    list_formset_set = []
    for match in matches:
        if match.isFinished == False:
            formset_set = MatchSetFormset(request.POST or None, instance=match, prefix="form-" + str(match.pk) + "-match")
            formset_set.match = match
            list_formset_set.append(formset_set)

    for formset_set in list_formset_set:
        id_match_submit = request.POST.get("match_pk")
        str_id_match_formet_set = str(formset_set.match.pk)
        if id_match_submit == str_id_match_formet_set:
            if formset_set.is_valid():
                formset_set.save()
                nb_set_winner_t1 = 0
                nb_set_winner_t2 = 0
                for set_match in formset_set:
                    if set_match.cleaned_data.get('scoreTeam1') == set_match.cleaned_data.get('scoreTeam2'):
                        nb_set_winner_t1 += 0
                        nb_set_winner_t2 -= 0
                    else:
                        if set_match.cleaned_data.get('scoreTeam1') > set_match.cleaned_data.get('scoreTeam2'):
                            nb_set_winner_t1 += 1
                            nb_set_winner_t2 -= 1
                        else:
                            nb_set_winner_t1 -= 1
                            nb_set_winner_t2 += 1   

                match = formset_set.cleaned_data[0].get('match')
                team1 = formset_set.cleaned_data[0].get('match').teams.first()
                team2 = formset_set.cleaned_data[0].get('match').teams.last()
                if nb_set_winner_t1 == nb_set_winner_t2:
                    team1.totalpoints += sport.nbPointPerDraw
                    team2.totalpoints += sport.nbPointPerDraw
                    team1.save()
                    team2.save()
                    match.isFinished = True
                    match.save()
                else:
                    if nb_set_winner_t1 > nb_set_winner_t2:
                        team1.totalpoints += sport.nbPointPerVictory
                        team1.nbVictory += 1
                        team2.totalpoints += sport.nbPointPerDefeat
                        team2.nbDefeat += 1
                        team1.save()
                        team2.save()
                        match.team1Win = True
                        match.isFinished = True
                        match.save()
                    else:
                        team1.totalpoints += sport.nbPointPerDefeat
                        team1.nbDefeat += 1
                        team2.totalpoints += sport.nbPointPerVictory
                        team2.nbVictory += 1
                        team1.save()
                        team2.save()
                        match.team2Win = True
                        match.isFinished = True
                        match.save()
                teams = Team.objects.filter(pool__in=pools).order_by('-totalpoints') #"-" means descending
                for index, team in enumerate(teams):
                    team.position = index + 1
                return redirect('matches_phase_manage_tournament', id=id, id_phase=id_phase)
            else:
                # reload formsets
                print("invalid")
  

环境:
  请求方法:POST
  要求网址:http://127.0.0.1:8000/tournament/admin-1/manage-phase/30-matches/
  Django版本:2.2
  Python版本:3.7.3
  已安装的应用程序:
  ['django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'users.apps.UsersConfig',
   'crispy_forms',
   '类别',
   'matches',
   “阶段”,
   '池',
   “规则”,
   '集合',
   '体育',
   “团队”,
   “比赛”,
   '页面',
   “健身房”,
   'fields']
  已安装的中间件:
  ['django.middleware.security.SecurityMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware']
  追溯:
  内部文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ core \ handlers \ exception.py”
    34. response = get_response(request)

  _get_response
中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ core \ handlers \ base.py”     115.响应= self.process_exception_by_middleware(e,request)

  _get_response
中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ core \ handlers \ base.py”     113. response = wrapd_callback(request,* callback_args,** callback_kwargs)

  matchs_phase_view中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ src \ pages \ views.py”
    370. print(formset)

  
中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ utils \ html.py”     388. klass。 str = lambda self:mark_safe(klass_str(self))

   str
中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ forms \ formsets.py”     64.返回self.as_table()

  as_table中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ forms \ formsets.py”
    404. forms =''.join(form.as_table()用于自身形式)

   iter
中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ forms \ formsets.py”     68.返回iter(self.forms)

  获取
中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ utils \ functional.py”     80. res =实例。 dict [self.name] = self.func(instance)

  表单中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ forms \ formsets.py”
    136. for i in range(self.total_form_count())]

  total_form_count中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ forms \ formsets.py”
    110.返回最小值(self.management_form.cleaned_data [TOTAL_FORM_COUNT],self.absolute_max)

  获取
中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ utils \ functional.py”     80. res =实例。 dict [self.name] = self.func(instance)

  在management_form中的文件“ C:\ Users \ 33643 \ Documents \ Projets Django \ TournamentManagerApp \ lib \ site-packages \ django \ forms \ formsets.py”
    92. code ='missing_management_form',

  异常类型:/ tournament / admin-1 / manage-phase / 30-matches /
处的ValidationError   异常值:['ManagementForm数据丢失或已被篡改']

1 个答案:

答案 0 :(得分:1)

检查您的HTML源,您将看到所有表单集都具有相同的前缀。因此,例如,您将拥有多个name属性设置为form-0-match的输入字段,该字段对应于表单集中第一个表单(索引0)的match字段。 / p>

在视图中使用多个表单集时(如here所述),您需要确保每个表单集具有不同的prefix

您可以通过将prefix参数传递给表单集初始化程序来设置前缀,例如:

formset_set = MatchSetFormset(request.POST or None, instance=match, prefix=f"form{match.pk}")