Python如何检查用户是否在视图页面中的组中

时间:2015-10-19 23:14:05

标签: python django

我正在构建一个可以创建和加入群组的项目。但是,我似乎无法弄清楚如果用户没有加入则如何显示加入按钮的逻辑,如果他们有加入则取消加入。我已附上模型和group.html页面。请在group.html文件中告诉我如何执行此操作。

谢谢。

models.py

from django.db import models

class User(models.Model):
    first_name = models.TextField(blank=False, max_length=20)
    last_name = models.TextField(blank=False, max_length=20)
    email = models.TextField(blank=False, max_length=20)
    password = models.TextField(blank=False, max_length=20)
    counter = models.IntegerField(blank=False, null=True)

class Group(models.Model):
    name = models.TextField(blank=False, max_length=50)
    description = models.TextField(blank=False, max_length=200)
    created_at = models.DateField()
    updated_at = models.DateField()
    creator = models.ForeignKey(User, related_name="creator")
    members = models.ManyToManyField(User, related_name="members")
    counter = models.IntegerField(blank=False, null=True)
    class Meta:
        db_table = 'groups'

groups.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>On Group {{group.id}}</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
    <a href="/logout">Logout</a>
    <span>|</span>
    <a href="/dashboard">Dashboard</a>
        <form action="/leave" method="post">
        {%csrf_token %}
        <input type="hidden" name="user_id" value="{{ request.session.user_id }}">
        <input type="hidden" name="group_id" value="{{ group.id }}">
        <input type="submit" value="Leave">
        </form>
        <form action="/join" method="post">
        {%csrf_token %}
        <input type="hidden" name="user_id" value="{{ request.session.user_id }}">
        <input type="hidden" name="group_id" value="{{ group.id }}">
        <input type="submit" value="Join">
        </form>
    <table class="table class table-striped table-bordered table-hovered table-hover">
        <thead>
            <tr>
                <th>Group Name</th>
                <th>Group Description</th>
                <th>Group Members</th>
                <th>Group Creator</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>{{group.name }}</a></td>
                <td>{{group.description}}</td>
                <td>{{group.members.count}}</td>
            {% if group.creator.id == request.session.user_id %}
                <td>You</td>
            {% else %}
                <td>{{group.creator.first_name}}</td>
            {% endif %}
            </tr>
        </tbody>
    </table>
    <h4>Members</h4>
    <table class="table class table-striped table-bordered table-hovered table-hover">
        <thead>
            <tr>
                <th>Member First Name</th>
                <th>Member Last Name</th>
            </tr>
        </thead>
        <tbody>
        {% for member in group.members.all %}
            <tr>
                <td>{{member.first_name}}</td>
                <td>{{member.last_name}}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

您可以在app文件夹中的templatetags文件夹下执行过滤:

from django import template
register = template.Library()

@register.filter
def has_group(user, group_name):
    return user.groups.filter(name=group_name).exists()

在您的模板中,例如:

{% if user|has_group:'groupName' %}
    <button></button>
{% else %}
    <p> Not in group </p>
{% endif %}

希望这有帮助。
编辑: 我可以问一个可能对你有帮助的问题。为什么你没有使用django.auth提供的用户模型?