T
T
tyilamg2019-04-25 17:32:44
Django
tyilamg, 2019-04-25 17:32:44

Why does it show in the header that the user is authorized?

Why doesn't the content of the header change when the user logs in? When I drive the correct data, it simply throws me to the main page, as it should, but does not change the content of the header, and if I drive the wrong data, it gives errors, in principle, as it should.
HTML

<header>
        <div class="w_gran">

            <div class="wrapper_head">
                <div id="logotype">
                    <a href="/index.html" class="head_logo_link">
                        {% load staticfiles %}
                        <img src="{% static 'img/logo.png' %}" alt="Logotype" class="head_img">
                    </a>
                </div>
                <ul id="head_nav">
                    <li class="head_li transition">
                        <a href="/news.html" class="head_link">
                            НОВОСТИ
                        </a>
                    </li>
                    <li class="head_li">
                        <a href="/post.html" class="head_link">
                            ПОСТЫ
                        </a>
                    </li>
                    <li class="head_li">
                        <a href="/result.html" class="head_link">
                            РЕЗУЛЬТАТ
                        </a>
                    </li>
                    <li class="head_li">
                        <a href="/info.html" class="head_link">
                            ИНФОРМАЦИЯ
                        </a>
                    </li>
                    <li class="head_li">
                        <a href="/contact.html" class="head_link">
                            КОНТАКТЫ
                        </a>
                    </li>
                    <li class="head_li_search">
                        <a href="" class="head_search_link">
                            {% load staticfiles %}
                            <img src="{% static 'img/icon_search.png' %}" alt="Search" class="head_search_img">
                        </a>
                    </li>
                </ul>
                <div class="auth_reg">
                   
                    {% if username %}

                    <a href="/post.html" class="head_auth">
                        Личный кабинет
                    </a>
                    <br>
                    <a href="/news.html" class="head_auth">
                        Выйти ({{ username }})
                    </a>

                    {% else %}

                    <a href="/login.html" class="head_auth">
                        Войти
                    </a>
                    <br>
                    <a href="/reg.html" class="head_reg">
                        Зарегестрироваться
                    </a>

                    {% endif %}

                </div>
            </div>
        </div>
    </header>

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'] = "User not found"
            return render_to_response('mainApp/login.html', args)
    else:
        return render_to_response('mainApp/login.html', args)
    
def logout(request):
    auth.logout(request)
    return redirect('/')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2019-04-25
@deepblack

{% if user.is_authenticated  %}
    {{ user.first_name }} {{ user.last_name }}
{% else %}
    <a href="{% url 'app:login' %}">{% blocktrans %}Log in{% endblocktrans %}</a>
{% endif %}

Read at your leisure, I recommend)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question