简体   繁体   中英

Couldn't Import GroupRequiredMixin

I want to check users if they are in a specific user group. If they are in that group they can view the "create product" page. Saw that documentation on web but couldn't use it properly by now.

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

I'm trying to use django-braces > GroupRequiredMixin but when I try to run my server I'm getting that error:

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)

My views.py looks like this:

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"

pip freeze:

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

Thank you for your time in advance.

If you want to use a class provided by django-braces, you need to import it from there, not from core Django which doesn't define it. As the docs you link to show, you should do from braces.views import GroupRequiredMixin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM