无法导入GroupRequiredMixin

时间:2019-03-27 07:32:21

标签: python django

我想检查用户是否在特定的用户组中。如果他们在该组中,则可以查看“创建产品”页面。在网上看到了该文档,但目前无法正确使用。

https://django-braces.readthedocs.io/en/latest/access.html#grouprequiredmixin

我正在尝试使用django-braces> GroupRequiredMixin,但是当我尝试运行服务器时却遇到了该错误:

File "C:\market\src\products\views.py", line 7, in <module>
    from django.contrib.auth.mixins import GroupRequiredMixin
ImportError: cannot import name 'GroupRequiredMixin' from 'django.contrib.auth.mixins' (C:\market\lib\site-packages\django\contrib\auth\mixins.py)

我的views.py看起来像这样:

from django.http import Http404
from django.shortcuts import render, get_object_or_404
from django.views.generic import TemplateView
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.contrib.auth.mixins import GroupRequiredMixin


from .forms import ProductModelForm, CategoryModelForm
from .models import Product, Category, Stocks


class ProductCreateView(GroupRequiredMixin, SubmitBtnMixin, CreateView):
    model = Product
    template_name = "form.html"
    form_class = ProductModelForm
    submit_btn = "Ürünü Kaydet"
    group_required = u"sellers"

点冻结:

Django==2.1.5
django-braces==1.13.0
django-polymorphic==2.0.3
pytz==2018.9

谢谢您的时间。

1 个答案:

答案 0 :(得分:2)

如果要使用django-braces提供的类,则需要从那里导入,而不是从未定义它的Django核心导入。正如您链接显示的文档一样,您应该执行from braces.views import GroupRequiredMixin