with <cursor_name> as cursor seem to work only once on server start

In my views.py file :

cursor_cloud = connections['cloud'].cursor()

def get_regions(request):

    if request.method == 'GET':
        query = """
            SELECT DISTINCT `Region` AS `Region`
            FROM `aws`
            WHERE `flag` = '1'
            LIMIT 0, 100
            """
      
        with cursor_cloud as cursor:
            cursor.execute(query)
            columns = cursor.fetchall()
            column = []
            for col in columns:
                column.append(col[0])
                
            json_obj = json.dumps(column)            
           
            result = HttpResponse(json_obj, 'application/json', charset='utf-8')            
            
            # cursor.close()

            return result

Oddest thing ever : first time, the URL http://localhost:8000/api/regions works giving a proper JSON, after which it fails saying that cursor was closed. To make it work again I had to restart the server.

Finally, I had to remove with cursor_cloud as cursor and make everything with cursor_cloud like cursor_cloud.execute(query) to make it work always.

Why is with cursor_cloud as cursor acting only once ?

What is cursor_cloud? Is that some third-party library? Where is it defined?
Is a new instance being created in the view?

Sorry - its in the beginning of my views.py :

cursor_cloud = connections['cloud'].cursor()

Updated original post.

If it’s at the module layer, then that’s a mistake. It needs to be inside the view.

At the module layer, that function is called once, when the module is imported. That means that you would be using the same instance of that cursor every time your view is called.