linuxfreak
linuxfreak

Reputation: 2166

Can we get request parameters from Django settings or other variables?

I see that the request parameters can be obtained in a viewset function using request variable. eg:

@detail_route(methods=['get'])
def mysampleviewsetfunction(self, request):
     print request

But, I want to be able to access request from some common variable. The purpose of that is to write a common function that can be called from all viewsets. This common function should be able to access request parameters depending upon the viewset that called it. But for some reason, I don't want to send the request parameter to this common function. My code should look like this:

@detail_route(methods=['get'])
def mysampleviewsetfunction(self, request):
      commonfunction()

def commonfunction():
      print xxxx.request

This 'xxxx' should be some global django variable that stores the current viewset's request. Is there any global variable that stores current viewset's request?

Upvotes: 0

Views: 150

Answers (1)

Sayse
Sayse

Reputation: 43300

it'd save me the trouble of modifying all the functions.

You wouldn't have to if you provide a default.

def commonfunction(request=None):

This of course means that any existing function calls won't be able to use the request until you update them so you'll need a check to make sure the request isn't none in this function.

There isn't any way you could use a global variable since that won't work asyncronously

Upvotes: 1

Related Questions