Getting Django function to return and render HTML page
<p>I have function (inside a <a href="https://docs.python.org/3/tutorial/classes.html#classes" rel="nofollow">class</a>) that returns <code>True</code> if a user <a href="https://docs.djangoproject.com/en/1.10/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated" rel="nofollow">is_authenticate()</a> and <a href="https://docs.djangoproject.com/en/1.10/ref/contrib/auth/#django.contrib.auth.models.User.is_active" rel="nofollow">is_active</a> but it is not returning <strong>HTML</strong> as I thought it would.</p>

<pre><code>class LoginDetails:
    def __init__(self, request):
        self.request = request
        self.template = 'myapp/errorpage.html'
        self.context = {}

    def user_is_logged_in(self):
        if self.request.user.is_authenticated() and self.request.user.is_active:
            print 'USER IS AUTHENTICATED!'
            return True
        print 'USER IS NOT AUTHENTICATED!'
        return render(self.request, self.template, self.context)
</code></pre>

<p><strong>views.py</strong>:</p>

<pre><code>def authUsers(request):
    logindetails = LoginDetails(request).user_is_logged_in()
    return HttpResponse("HTML Error Page Not Rendered")
</code></pre>

<p>How do I get the function to display/render error page without me doing it in <em>views.py</em>? Example of what I want to achieve is Stackoverflow's <a href="https://stackoverflow.com/questions/397288888888888882033/android-retrotfit-modify-fields-of-object-after-receive">Page Not Found</a>. </p>

<p>First of all your class really is redundant, calling it takes one line and modifying your view is also one line</p>

<pre><code>from django.http import HttpResponse, Http404

def authUsers(request):
   if request.user.is_authenticated() and request.user.is_active:
       raise Http404
   else :
       return render(...)
</code></pre>

<p>Using that class merely complicates matters. But if you really wanted to, you could modify it like this: </p>

<pre><code>class LoginDetails:
    def __init__(self, request):
        self.request = request
        self.template = 'myapp/errorpage.html'
        self.context = {}

    def user_is_logged_in(self):
        if self.request.user.is_authenticated() and self.request.user.is_active:
            return False

        return render(self.request, self.template, self.context)


def authUsers(request):
    logindetails = LoginDetails(request).user_is_logged_in()
    if not logindetails:
        return HttpResponse("HTML Error Page Not Rendered")
    else:
        return logindetails
</code></pre>

python django