想要打印用户所属的所有组

时间:2019-02-24 14:14:06

标签: django

**Models.py**

class User(auth.models.User,auth.models.PermissionsMixin):

    def __str__(self):
        return "@{}".format(self.username)

    def get_absolute_url(self):
        return reverse("accounts:login")


class Group(models.Model):
    name = models.CharField(max_length = 255,unique = True)
    slug = models.SlugField(allow_unicode = True,unique = True)
    description = models.TextField(default = '')
    members = models.ManyToManyField(User,related_name = "group")

    def save(self,*args,**kwargs):
        self.slug = slugify(self.name)
        super(Group,self).save(*args,**kwargs)

    def get_absolute_url(self):
        return reverse('groups:single',kwargs = {'slug':self.slug})

    def __str__(self):
        return self.name

**Views.py**

class ListGroups(ListView):
    model = Group

**Html code**

<div class="col-md-8">
<div class="list-group">
  {% if user.is_authenticated %}
  <h2>Your Groups!</h2>

    {% if user.group.count == 0 %}
      <p>You have not joined any groups yet! <p>
    {% else %}
      {% for group in user.group.all %}
      <a class="list-group-item" href="{% url 'groups:single' slug=group.slug %}">
        <h3 class="list-group-item-heading">{{group.name}}</h3>
        <div class="list-group-item-text container-fluid">
          {{group.description|safe}}
          <div class="row">
              <div class="col-md-4">
                  <span class='badge'>{{group.members.count}}</span>
                  member{{group.members.count|pluralize}}
              </div>
              <div class="col-md-4">
                  <span class='badge'>{{group.posts.count}}</span>
                  post{{group.posts.count|pluralize}}
              </div>
            </div>
        </div>
      </a>
      {% endfor %}
    {% endif %}
  {% endif %}
</div>

据我说,它应该打印所有组以及当前登录用户所属的详细信息,但它不打印任何内容。我不知道这段代码有什么问题。我尝试了其他方法,但没有任何效果。有人帮忙吗? 提前致谢。

1 个答案:

答案 0 :(得分:0)

在模板中没有名为user的变量,因此您编写的所有内容都可能无法使用。

但是,我们可以通过首先对Group进行一些修改来获取已登录用户所属的ListsGroup的列表:

from django.contrib.auth.mixins import LoginRequiredMixin

class ListGroups(LoginRequiredMixin, ListView):
    model = Group

    def get_queryset(self):
        return Group.objects.filter(members=self.request.user)

现在,我们可以像这样渲染

<div class="col-md-8">
<div class="list-group">
  <h2>Your Groups!</h2>
    {% for group in object_list %}
    <a class="list-group-item" href="{% url 'groups:single' slug=group.slug %}">
      <h3 class="list-group-item-heading">{{group.name}}</h3>
      <div class="list-group-item-text container-fluid">
        {{group.description|safe}}
        <div class="row">
            <div class="col-md-4">
                <span class='badge'>{{group.members.count}}</span>
                member{{group.members.count|pluralize}}
            </div>
            <div class="col-md-4">
                <span class='badge'>{{group.posts.count}}</span>
                post{{group.posts.count|pluralize}}
            </div>
          </div>
      </div>
    </a>
  {% empty %}
    <p>You have not joined any groups yet! <p>
  {% endfor %}
</div>