简体   繁体   中英

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 Ie the function doesn't accept any GET / POST requests.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM