No Name
No Name

Reputation: 98

How can a view call a function (which is not a view) in django?

Is it possible to create a view/function in django that is callable only from another function I.e. the function doesn't accept any GET/POST requests.

Upvotes: 2

Views: 5640

Answers (1)

Laurits L. L.
Laurits L. L.

Reputation: 417

You can just call a function in your view as you would normally do, and you can also pass all the arguments you want. It will behave just like a normal function, even though it is called from a view (You can see that the normal function is normal, because it doesn’t return an HttpResponse object).

from django.shortcuts import render

def example_view(request):
    """Example."""
    print_curr_user(request)

    return render(request, ‘some_app/some_html.html’)

# Normal function.
def print_curr_user(request):
    """Example: print current user from request object."""
    print(request.user)

Upvotes: 1

Related Questions