在 django 中使用组模型、ManyToMany 字段的不良 for 循环



我想在 for 循环中显示 1 个按钮,但我得到的按钮数量与我创建的组数量一样多。 所以.. 当我创建 3 个组时,我是其中之一,我在我拥有的每个组下都有 3 个按钮。问题出在我的代码中的第一个循环,但我不知道如何解决这个问题。

问题在循环中:

{% for z in mem %}

当我创建任何会员资格时,它就像:

m = Membership.objects.create(person="damian", group="name_group", leader=True)  / or False

感谢您的任何帮助!

组.html:

{% for g in gr %}
           <div class="jumbotron">
            <div class="jumbo2">
              <form method="POST" class="post-form"> {% csrf_token %}
                <p id="name"><b>Group's name:</b> {{g.name}}</p><br>
                {% for member in g.members.all %}
                  <p><b>Member:</b> {{member.name}} </p>
                {% endfor %}

                <br>
                <span class="desc2">Group's description:</span>
                <p id="desc">{{g.description}}</p><br>
                {% for z in mem %}
                  {% if z.leader == False %}
                    <button style="float: right" type="submit" name = "leave"  value = "{{g.name}}" class="save btn btn-default">Leave</button>
                  {% elif z.leader == True %}
                    <button style="float: right" type="submit" name = "delete"  value = "{{g.name}}" class="save btn btn-default">Delete</button>
                  {% endif %}
                {% endfor %}

              </form>
              <br><br>
              <p>
              {% if messages %}
                {% for message in messages %}
                  <p>{{ message }}</p>
                {% endfor %}
              {% endif %}
              </p>
            </div>
          </div>

          {% endfor %}

views.py:

    cvs = Cv.objects.all()
    cv = Cv.objects.filter(author = request.user)
    per = Person.objects.all()
    gr = Group.objects.filter(members__name=request.user)
    perr = Person.objects.filter(name=request.user)
    mem = Membership.objects.filter(group = gr, person = perr)
    form = GroupForm()

    context = {
        'gr': gr,
        'per':per,
        'mem':mem,
        'form': form,
        'cvs':cvs,
        'cv':cv,
    }
    return render(request, 'groups.html', context)

models.py:

class Person(models.Model):
    name = models.CharField(max_length=128)
    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership')
    description = models.TextField(max_length=350)
    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Membership(models.Model):
    person = models.ForeignKey(Person)
    leader = models.BooleanField(default=False)
    group = models.ForeignKey(Group)

好的,我替换了我的组并仅使用成员对象。这是我的工作代码:

          {% for z in mem %}
           <div class="jumbotron">
            <div class="jumbo2">
              <form method="POST" class="post-form"> {% csrf_token %}
                <p id="name"><b>Group's name:</b> {{z.group}}</p><br>
                {% for member in z.group.members.all %}
                  <p><b>Member:</b> {{member.name}} </p>
                {% endfor %}

                <br>
                <span class="desc2">Group's description:</span>
                <p id="desc">{{z.group.description}}</p><br>

                      {% if z.leader == False %}
                        <button style="float: right" type="submit" name = "leave"  value = "{{z.group}}" class="save btn btn-default">Leave</button>
                      {% elif z.leader == True %}
                        <button style="float: right" type="submit" name = "delete"  value = "{{z.group}}" class="save btn btn-default">Delete</button>
                      {% endif %}
              </form>
              <br><br>
              <p>
              </p>
            </div>
          </div> 
          {% endfor %}

相关内容

  • 没有找到相关文章

最新更新