In a GeoNode istance some layers have the typename attribute set to null, so the line
r = requests.get(f"{baseurl}gwc/rest/layers/{layer.typename}.xml", auth=HTTPBasicAuth(user, passwd))
will likely fail with a 404.
The create_tile_layers command should fallback to the resource.alternate value in case dataset.typename is null.