Q:

request.user.is_authenticated working only in the home page

I am creating a website where a user can upload documents.  If user is logged out he can view other's document but not download it 
if user is logged in he can download the document from the toolbar in pdf viewer
Even when I am logged out request.user.is_authenticated is false in home page but its true in other pages.  So the download button does not hide nor a login button shows up. 
I have tried using user.is_authenticated but it was returning true all the time
the if else condition works fine in the index page but not in doc_detail.html
urls.py
re_path(r'^all_files/(?P<doc_id>[0-9]+)/$',views.doc_detail,name = 'doc_detail'),
path('login/',auth_views. LoginView.as_view(template_name='homework/login.html'),name = 'login'),

views.py
def doc_detail(request,doc_id):
    template = loader.get_template('homework/doc_detail.html')
    doc = get_object_or_404(Document, pk = doc_id)
    context = {
    'doc':doc
    }
    return HttpResponse(template.render(context,request))

doc_detail.html
{{ doc.user.username }}
{{ doc.title }}
{{ doc.doc_type }} <br>

{% if request.user.is_authenticated %}
    <iframe src="{{ doc.document.url }}#toolbar=1&navpanes=0&scrollbar=1" type="application/pdf" width = "80%" height = "600px" />
{% else %}
    <iframe src="{{ doc.document.url }}#toolbar=0&navpanes=0&scrollbar=0" type="application/pdf" width = "80%" height = "600px"  />
    <a href=" url 'homework:login'">login</a>
{% endif %}

index page
<! DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
This is the homework page implement upload-routine explore-routine here  
{% if request.user.is_authenticated %} 
the user is {{ request.user.email }} {{ request.user.profile.institution }}

{% else %}
user not logged in
{% endif %}

{% if request.user.is_authenticated %}
    <a href="{% url 'homework:logout' %}">logout</a>
    <a href="{% url 'homework:update' %}">update</a>
    <a href="{% url 'homework:upload' %}">upload</a>
    <a href="{% url 'homework:my_uploads' %}">my uploads</a><br>
{% else %}
    <a href="{% url 'homework:login' %}">login</a><br>
{% endif %}

<a href="{% url 'homework:all_files' %}">all files</a>
</body>
</html>

A:

As pointed out in the comments, you should be using the render shortcut; it seems you are confusing that with the render method of a template.  There's no reason to be using the template object directly here, or just about anywhere in standard Django code. 
def doc_detail(request, doc_id):
    doc = get_object_or_404(Document, pk=doc_id)
    context = {
        'doc':doc
    }
    return render(request, 'homework/doc_detail.html', context)

