[Python] Django decorator import issue: 'cannot import name' when used in views.py

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página