在上下文中传递用户的组名并在django中访问模板

时间:2016-10-24 07:57:51

标签: django templates django-class-based-views

我在django app中使用了mixins和基于类的视图。我还创建了一个名为Instructors的组,只允许属于该组的用户具有编辑,添加和删除课程对象的特定权限。 在负责呈现当前用户创建的课程的 ManageCourseList View 中,我尝试覆盖 get_context_data 传递包含用户所属组的其他" group_list' 参数的方法。 我试图在模板ManageCourseList视图渲染中访问它。 问题是虽然当前用户属于Instructors组,但它仍然呈现else条件数据。 请帮忙,我的文件:

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 %}

1 个答案:

答案 0 :(得分:1)

首先,这种方法毫无意义;您只需访问模板中的user.groups即可。

其次,组中的每个项目都是一个Group对象,而不是一个字符串。所以你的病情永远不会成功;你需要与group.name进行比较。而且,教师将成为第一组还远未确定。

要解决这个问题,我会重写你的get_context_data来添加一个教师标志而不是组列表:

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

现在在模板中:

{% if user_is_instructor %}