1

I have created one library file where I have created one function suppose :

def myfunc(param1):
    ----
    --- do something
    ----

Now If I require requests object (to get details of user or something like that), How can I get requests object without passing parameter in my function.

Note : As of now I am adding requests in my parameters (shown as below) but is there any other method as If I am calling this function to any other function I might not have requests object.

def myfunc(requests,param1):
    ----
    --- do something
    ----
Kamesh Jungi
  • 6,203
  • 6
  • 39
  • 50
  • try this one [Can a variable number of arguments be passed to a function?](http://stackoverflow.com/questions/919680/can-a-variable-number-of-arguments-be-passed-to-a-function). Hope this is help you. – NIKHIL RANE Nov 16 '16 at 12:05
  • 2
    No, of course there is no way to get an object without passing it as a parameter. Don't even try. – Daniel Roseman Nov 16 '16 at 12:05
  • @DanielRoseman I am just confirming the same. Can you brief me about the reason. – Kamesh Jungi Nov 16 '16 at 12:07
  • 1
    It is probably better to pass in the details of the user as a parameter, if that is what the function needs. – RemcoGerlich Nov 16 '16 at 13:47
  • In general it is a sign of bad design to need the request globally, it means your modules are coupled too much. But sometimes there is a legitimate reason (e.g. a custom manager to use on models that returns only objects the current user is allowed to see) and for that I have used django-tls in the past (https://pypi.python.org/pypi/django-tls), it can make request a global. – RemcoGerlich Nov 16 '16 at 13:49
  • @RemcoGerlich can you provide me one example how can I make my request global with django-tls (currently I am unable to find anything related to that) – Kamesh Jungi Nov 17 '16 at 10:15
  • It's literally only the three things in https://pypi.python.org/pypi/django-tls (install django-tls, add the middleware, then from tls import request). Use sparingly! – RemcoGerlich Nov 17 '16 at 10:18

2 Answers2

1

I have get solution that I can install a middleware like TLSRequestMiddleware where we need to install :

pip install django-tls

and then,

# settings.py
MIDDLEWARE_CLASSES = (
    'tls.TLSRequestMiddleware',
    ...
)

In case in django version 1.10, you update package as :

try:
    from threading import local
except ImportError:
    from django.utils._threading_local import local

_thread_locals = local()

def get_current_request():
    return getattr(_thread_locals, 'request', None)

def get_current_user():
    request = get_current_request()
    if request:
        return getattr(request, 'user', None)

class ThreadLocalMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        _thread_locals.request = request
        return self.get_response(request)

Also there is option like

pip install django-crum

and then,

# settings.py
MIDDLEWARE_CLASSES = (
     'crum.CurrentRequestUserMiddleware',
    ...
)

I hope this will help !

Kamesh Jungi
  • 6,203
  • 6
  • 39
  • 50
0

You can pass parameter in URL

In your urls.py :

(r'^your_url/(?P<param1>\w{0,50})/$', views.your_view),

In your views.py :

def your_view(request, param1):
    # Do something

If you want to pass request in function. Call function and pass request :

your_view(request, param1)

In your case :

def myfunc(param1, request=None):
    ----
    --- do something
    ----

Call function if you have a request

myfunc(param1, requests)

If request is not available then simply call

myfunc(param1)
Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
NIKHIL RANE
  • 4,012
  • 2
  • 22
  • 45