Django – after sign-in template don't know that user is authenticated

Question:

Below code probably works (no errors present):

views.pl

class SignInView(View):

    def get(self, request):
        return render(request, "signin.html")

    def post(self, request):
        user = request.POST.get('username', '')
        pass = request.POST.get('password', '')

        user = authenticate(username=user, password=pass)

        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/')
            else:
                return HttpResponse("Bad user.")
        else:
            return HttpResponseRedirect('/')

….but in template:

{% user.is_authenticated %}

is not True. So I don’t see any functionality for authenticated user.

What is the problem?

Asked By: PrertoQuebas

||

Answers:

You should do like {% if request.user.is_authenticated %} or {% if user.is_authenticated %}

Answered By: anjaneyulubatta505

You should do something like:

{% if request.user.is_authenticated %}
    <!-- code for authenticated user -->
{% else %}
    <!-- code for unauthenticated user -->
{% endif %}

I could see another problem in views, pass is a reverse keyword in Python, you should also change variable name.

Answered By: Sunderam Dubey