0

I have a function that returns a render of a html (function 1). I want to change a global variable using formula 1 and the access this edited global variable in another formula (formula 2). In order to do this I think I have to return the global variable in formula 1, so the edited version can be accessed in formula 2. My problem is that function 1 already returns a render request.

global_variable = 0

def function1(request):

    global_variable = 10 

    return render(request, 'index.html', {})



def function2(request):

    print(global_variable)

excel = open(path, 'rb')
response = HttpResponse(excel.read(), content_type="app/vnd.openxmlformat.spreadsheetml.sheet")
response['Content-Disposition'] = 'attachment; filename=' + os.path.basename("Excel.xlsx")
return response

I have tried to just add in the global variable at the end of the function like so:

def function1(request):

    global_variable = 10 

    return render(request, 'index.html', {}), global_variable

except it causes the error 'tuple' object has no attribute 'get'

kitchen800
  • 197
  • 1
  • 12
  • 36
  • `function1` doesn't use a global variable in the first place: it creates a local variable that shadows the global variable. – chepner Dec 09 '21 at 14:51
  • The idea of passing a value as an argument and returning a (possibly modified) value, though, is very close to the idea of using monads in functional programming to simulate mutable global variables. – chepner Dec 09 '21 at 14:53

0 Answers0