[Answer]-Call variable from method inside the method



One way to achieve this would be to create a helper method that returns the report or the context needed by both the methods. The issue here is you are returning a HTTPResponse object, and it is cleanest to use helper method here:

def get_report(request):
    #do more processing here.
    #returning a dict is probably safest here, because, in the calling method, `.get()` would not throw an error if key is not present. 
    return {'report': report, 'somevar': somevar}

def followup(request):
     marklist = get_report(request).get('somevar')
     return render(request, 'incident/followup.html',{'somevar':marklist})

def report_template(request):
    report = get_report(request).get('report')
    return render(request, 'incident/print.html',


You can not do that with views, you have to look up for templatetags, which is a better way to do so in Django (a view accepts a Request argument and returns an HttpResponse variable).

See https://docs.djangoproject.com/en/dev/howto/custom-template-tags/.

Using templatetags makes your code reusable, by factoring what you do in views.

Leave a comment