R
R
Rassul Nassyrov2020-09-15 11:54:19
Django
Rassul Nassyrov, 2020-09-15 11:54:19

Django login problem, how to implement in my case?

I have an account model with user data. When I check in views.py if there is a login and password in the database similar to the one entered by the user on the site, if there is such data, I create contextlogin = request.POST['login'], and pass this variable to html and through jinja I check for data the name of this person. But the problem is that each user logs in and the contextlogin is overwritten and other users change their data after the page is reloaded, how can this be fixed?

Views.py :

def main(request):
    global contextlogin
    global context
    if request.method == "POST":
        for i in account.objects.all():
            if i.account_login == request.POST['login']:
                if i.account_password == request.POST['password']:
                    context = account.objects.all()
                    contextlogin = request.POST['login']
    return render(request, "fr/main.html")

def profile(request):
    global context
    global contextlogin
    return render(request, "fr/profile.html", {'context': context, 'contextlogin': contextlogin})


main.html:

<form action = "" method = 'POST'>
    {% csrf_token %}
    <div id = 'login'>
        <p title = 'Вход в аккаунт' style = 'text-align: center; margin-left: -30px;'>Вход в аккаунт</p>
        <input placeholder="Ваш логин" id = 'id_login' name = 'login' type = 'text' style = 'width: 200px; height: 35px; margin-left: 130px;' required>
        <input placeholder="Ваш пороль" id = 'id_password' name = 'password' type = 'password' style = 'width: 200px; height: 35px; margin-left: 130px; margin-top: 10px;' required>
    <button title = 'Проверить и войти' type = 'submit' style = 'background-color: rgb(26, 66, 18); color: red; width: 220px; height: 35px; margin-left: 130px; margin-top: 10px;' required>Войти</button>
        <br>
        <br>
        <a title = 'Восстоновите ваш аккаунт!' href = "{% url 'forgot' %}" style = 'color: black; margin-left: 175px; margin-top:10px;'>Забыли пороль?</a>
        <br>
        <br>
        <a title = 'Впервые тут? Зарегестрируйтесь..' href = "{% url 'addaccount' %}" style = 'color: black; margin-left: 172px; margin-top:10px;'>Создать аккаунт</a>
    </div>
    </form>


profile.html:
{% for i in context %}
        {% if i.account_login == contextlogin %}
        <p style = 'color:rgb(10, 10, 10);  margin-top: 10px; margin-left: 500px;'>Ваш профиль</p>
        <div id = "Widget">
            <img style = 'margin-left: 52px; margin-top: -10px;' onclick = "" width = '40' height = '30' src = {{i.account_image.url}}>
            <br>
            <a style = 'text-decoration: none; margin-left: 18px; color:rgb(247, 242, 242);' href = ''>Мой профиль</a>
            <hr>
            <img style = 'margin-left: 52px; margin-top: 5px;' onclick = "location.href = ''" width = '40' height = '30' src = '../../../static/images/postinfo.png'>
            <a style = 'text-decoration: none; margin-left: 38px; color:rgb(247, 242, 242);' href = ''>Главная</a>
            <hr>
            <div id = "Profile">
                <img width = '200' height = '200' src = {{i.account_image.url}}>
                <br>
                <form action = '' method = "POST">
                {% csrf_token %}
                <div class="input__wrapper">
                    <input name="file" type="file" name="file" id="input__file" class="input input__file" multiple required>
                    <label for="input__file" class="input__file-button">
                    <span class="input__file-button-text">Выберите фото</span> 
                    </label>
                 </div>
                <button style = 'width: 120px; height: 30px; margin-left: 40px; margin-top: -9px; background-color: rgb(17, 240, 9);' type = 'submit'>Сохранить</button>
                 </form>
                 <br>
                <p style = 'margin-left: 210px; margin-top: -285px;'> <strong>{{i.account_name}} {{i.account_surname}}</strong></p>
                <div id = 'Tick'></div>
                <p style = 'margin-left: 210px; margin-top: 5px;'> Логин: </p>
                <p style = 'color: rgb(10, 34, 250); margin-left: 380px; margin-top: -35px;'> {{i.account_login}}</p>
                <p style = 'margin-left: 210px; margin-top: 5px;'> Email: </p>
                <p style = 'color: rgb(10, 34, 250); margin-left: 380px; margin-top: -35px;'> {{i.account_email}}</p>
                <p style = 'margin-left: 210px; margin-top: 5px;'> Номер телефона: </p>
                <p style = 'color: rgb(10, 34, 250); margin-left: 380px; margin-top: -35px;'> {{i.account_number}}</p>
                <p style = 'margin-left: 210px; margin-top: 5px;'> Пороль: </p>
                <p onclick = "location.href = ''" style = 'color: rgb(250, 3, 3); margin-left: 380px; margin-top: -35px;'> редактировать</p>
                <p style = 'margin-left: 210px; margin-top: 5px;'> Ваш возраст: </p>
                <p style = 'color: rgb(10, 34, 250); margin-left: 380px; margin-top: -35px;'> {{i.account_old}} лет</p>
                <div id = "Tick"></div>
                <p style = 'margin-left: 210px; margin-top: 5px;'> Страна: </p>
                <p style = 'color: rgb(10, 34, 250); margin-left: 380px; margin-top: -35px;'> {{i.account_country}}</p>
                <p style = 'margin-left: 210px; margin-top: 5px;'> Город: </p>
                <p style = 'color: rgb(10, 34, 250); margin-left: 380px; margin-top: -35px;'> {{i.account_city}}</p>
                <p style = 'margin-left: 210px; margin-top: 5px;'> Пол: </p>
                <p style = 'color: rgb(10, 34, 250); margin-left: 380px; margin-top: -35px;'> {{i.account_sex}}</p>
                <p style = 'margin-left: 210px; margin-top: 5px;'> Увлечения: </p>
                <p style = 'color: rgb(10, 34, 250); margin-left: 380px; margin-top: -35px;'> {{i.account_hobby}}</p>
                <p style = 'margin-left: 210px; margin-top: 5px;'> Сайт: </p>
                <p onclick = "location.href = '{{i.account_weburl}}'" style = 'color: rgb(250, 10, 62); margin-left: 380px; margin-top: -35px;'> {{i.account_webname}}</p>
                <div id = 'Tick'></div>
                <br>
                <a href = '' style = 'text-align: center; margin-left: 300px;'> Редактировать аккаунт </a>
            </div>
        </div>
        {% endif %}
        {% endfor %}


I hope you got what I meant()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-09-15
@nssr02

global in Django? Seriously?
The entire required context must either be calculated each time, or it is already in request, the same request.user, but for this you need to use the standard authentication system.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question