S
S
Stanislav Yaroslavtsev2019-08-21 08:11:03
Django
Stanislav Yaroslavtsev, 2019-08-21 08:11:03

Why doesn't django show variable in templates?

Why is my username variable displayed in only one template and not all of them?
Code:
views.py

def login(request):
    args = {}
    args.update(csrf(request))
    if request.POST:
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        user = auth.authenticate(username=username, password=password)
        if user is not None:
            auth.login(request, user)
            return redirect('/')
        else:
            args['login_error'] = "Пользователь не найден"
            return render(request, 'login.html', args)

    else:
        return render(request, 'login.html', args)

Code in templates:
{% if username %}
                <li class="nav-item active">
                <a href="/auth/logout/"><button type="button">Выйти({{ username }})</button></a>
                </li>
{% else %}
                <li class="nav-item active">
                <a href="/auth/login/"><button type="button">Войти</button></a>
                </li>
{% endif %}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question