1

when trying to start the server it cannot find the decorator


from django.contrib.auth.decorators import login_required, user_passes_test, staff_member_required

...


@staff_member_required 
def delete_service_view(request, service_id):
    service = get_object_or_404(Service, pk=service_id)
    service.delete()
    return redirect('index') 

@staff_member_required 
def toggle_trusted_from_service_view(request, user_id): 
    if request.method == 'POST':
        try:
            user = User.objects.get(pk=user_id)
            user.isTrusted = not user.isTrusted
            user.save()
            return redirect('service_detail', service_id=request.POST.get('service_id')) 
        except User.DoesNotExist:
            return HttpResponseForbidden("User not found.")
    else:
        return HttpResponseForbidden("Invalid enquiry method.")

When i try python manage.py runserver


  File "C:\Users\alexe\djangoshop\dshop\main\views.py", line 3, in <module>
    from django.contrib.auth.decorators import login_required, user_passes_test, staff_member_required
ImportError: cannot import name 'staff_member_required' from 'django.contrib.auth.decorators' (C:\Users\alexe\djangoshop\dshop\dshop_new_env\Lib\site-packages\django\contrib\auth\decorators.py)

I've already recreated the environment and reinstalled all dependencies

1 Answer 1

0

The decorator does not reside there, it is made for the admin site, so:

from django.contrib.admin.views.decorators import staff_member_required

By default, this also redirects to the admin login if the user is not a staff user.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.