[Django]-Django Global function and variable

11👍

Make it a function your views use; return the shop_id value from the function:

def get_shop_id(request):
    if "current_shop" in request.session:
        return request.session['current_shop']

    return 1 if request.user.is_superuser else 2

and in your views:

shop_id = get_shop_id(request)

If you put this in a separate module; say utils.py, you can import it with:

from projectname.utils import get_shop_id

or using a relative import:

from .utils import get_shop_id

provided utils.py lives in the same package as your views.

Leave a comment