[Answered ]-How I validate if user belongs and if not he is not allowed in Django

1👍

This worked for me.

@permission_classes((IsAuthenticated,))
def Get_UserRequest(request, Hospital_id):
    # if not request.user.is_authenticated:
    #     return redirect('login')
    perm = 1
    user = request.user
    if request.user.is_authenticated and request.method == "GET":
        user_sector = user.FKLab_User.id

        requests = []
        if Hospital_id != user_sector: 
            perm = 0

        if perm == 0:
            error = "You are not allowed."
            return render(request, "error/error.html", {'error':error})
            
    requests = RequestHepatoPredict.objects.filter(Hospital_id=Hospital_id)
    return render(request, 'user_profile/requests.html', {'requests': requests})
👤Max

Leave a comment