In django function base view how to inherit some opertion that are used in all views functions?

Viewed 27

I am using django function base views these operation needed in all views.

    resourceList=resource.objects.all()
    params={
     'resourcelist':resourceList
    }
    return render(request, html file, params)

One solution is that I will type in all my views function. Anyone can help me with a suitable answer for this?

1 Answers

You can make a function of the repeated code and call it where you need it. In your case, you can do:

def common_code_function(request,html_file_name):
    resourceList=resource.objects.all()
    params={
        'resourcelist':resourceList
    }
    return render(request, html_file_name, params)
Related