Ujjawal Malik
Ujjawal Malik

Reputation: 65

Passing group name of a user in context and accessing in template in django

I have used mixins with class-based views in my django app. I also have created a group named Instructors to allow only user belonging to this group to have certain permissions which are to edit, add and delete a course object. In the ManageCourseList View which is responsible for rendering the courses created by the current user, I tried to override the get_context_data method to pass an additional 'group_list' argument which contains the groups the user belongs to. I tried to access it in the template ManageCourseList view renders. The problem is though the current user belongs to Instructors group but still it renders the else condition data. Please help,My files:

Views.py

from django.shortcuts import render
from django.core.urlresolvers import reverse_lazy
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView,UpdateView,DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin,PermissionRequiredMixin

from .models import Course


class OwnerMixin(object):
    def get_queryset(self):
        qs = super(OwnerMixin,self).get_queryset()

        return qs.filter(owner=self.request.user)

class OwnerEditMixin(object):
    def form_valid(self,form):
        form.instance.owner = self.request.user
        return super(OwnerEditMixin, self).form_valid(form)

class OwnerCourseMixin(OwnerMixin,LoginRequiredMixin):
    model = Course
    fields = ['subject','title','slug','overview'] # why use this attribute?
    success_url = reverse_lazy('courses:manage_course_list') # use of it?

class OwnerCourseEditMixin(OwnerCourseMixin,OwnerEditMixin):
    fields = ['subject','title','slug','overview']
    success_url = reverse_lazy('courses:manage_course_list')
    template_name = 'courses/manage/course/form.html'

class ManageCourseListView(OwnerCourseMixin,ListView):
    template_name = 'courses/manage/course/list.html'

    def get_context_data(self, **kwargs):
        context = super(ManageCourseListView,self).get_context_data(**kwargs)
        context['group_list'] = self.request.user.groups.all()
        return context

list.html

{% extends "base.html" %}

{% block title %}My courses{% endblock %}

{% block content %}
    {% if group_list.0 == "Instructors" %}
    <h1>My courses</h1>
        <div class="module">
            {% for course in object_list %}
                <div class="course-info">
                    <h3>{{ course.title }}</h3>
                    <p>
                        <a href="{% url 'courses:course_edit' course.id %}">Edit</a>
                        <a href="{% url 'courses:course_delete' course.id %}">Delete</a>
                    </p>
                </div>
            {% empty %}
                <p>You haven't created any courses yet.</p>
            {% endfor %}
            <p>
                <a href="{% url 'courses:course_create' %}" class="button">Create new course</a>
            </p>
        </div>

    {% else %}
    <h1>No courses to display</h1>
    {% endif %}

{% endblock %}

Upvotes: 0

Views: 1141

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599788

Firstly, that method is pointless as it is; you can simply access user.groups in the template.

Secondly, each item in group is a Group object, not a string. So your condition will never succeed; you would need to compare against group.name. Also, it's far from certain that instructors will be the first group.

To solve this, I would rewrite your get_context_data to add an instructors flag rather than the group list:

def get_context_data(self, **kwargs):
    context = super(ManageCourseListView,self).get_context_data(**kwargs)
    context['user_is_instructor'] = self.request.user.groups.filter(name='Instructors').exists()
    return context

and now in the template:

{% if user_is_instructor %}

Upvotes: 2

Related Questions