create group

Date: April 20th 2016
Last updated: April 20th 2016

Create group

# python3 shell
from django.contrib.auth.models import User, Group

new_group, created = Group.objects.get_or_create(name='new_group')

#<Group: new_group>

add user to a group

python3 shell
# get group
group = Group.objects.get(name='new_group')
# get user
u = User.objects.get(pk=2)
# add user to group

search all users in a group

# list all group members
group.user_set.all() # [<User: Ray>]

search groups that a user is in

u = User.objects.get(pk=2) 
#<User: Ray>
#[<Group: new_group>]

access groups in html template

def groups(request):
    surfer_list = Surfer.objects.all()
    groups_list = Group.objects.all()
    context = {'surfer_list': surfer_list, 'groups_list': groups_list }
    return render(request, 'surferprofile/groups.html', context)


<div style="margin: 5%;">
            {% for group in groups_list %}
            {% if user in group.user_set.all %}
            OR populate a table of group names if the user is not in the group 
            {% if user not in group.user_set.all %}
        {% endif %}
        {% endfor %}

results matching ""

    No results matching ""