Flask - неверный URL при входе - PullRequest
       3

Flask - неверный URL при входе

0 голосов
/ 14 февраля 2020

У меня сегодня проблема, и мне нужно решить ее, чтобы перейти к другой задаче. Когда я хочу войти в систему, соединение выполняется без проблем, однако при подключении URL остается в «http://127.0.0.1/login», но отображает индекс моей страницы. html по этой ссылке. И я не понимаю, почему я не могу напрямую go в мою директорию "http://127.0.0.1/index" напрямую с моим индексом. html. В конце моей функции входа в систему вместо "return render_template ('index. html', ** templateData)" я ставлю "index ()" напрямую, как при выходе из системы, но у меня есть код ошибки 500

Я могу поделиться всем своим каталогом python, если вам нужно.

С уважением (извините за мой плохой английский sh ..!)

@app.route("/")
@app.route("/index")
def index():
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        if app.NOM == '':
            return render_template('login.html', login=1)
        else:
            var = "SELECT users.prenom, users.nom, data.bpm, data.oxy, data.chute FROM data, users WHERE users.ID = '" + str(app.ID) + "' AND data.User = '" + str(app.ID) + "' ORDER BY data.User DESC LIMIT 1 "
            try:
                cur = bdd_login()
            except pymysql.Error as e:
                return session_out(e)
            result = cur.execute(var)
            cur.close()
            print(result)
            if not result:
                bpm = 0
                oxy = 0
                chute = 0
            else:
                for row in cur:
                    print(row)
                bpm = row[2]
                oxy = row[3]
                chute = row[4]
                templateData = {'prenom': app.PRENOM, 'nom': app.NOM, 'rank': app.RANK, 'bpm': bpm, 'oxy': oxy}
                if bpm < 40 or bpm > 100:
                    if oxy < 80:
                        if chute == 0:
                            return render_template('index.html', **templateData, state_bpm=0, state_oxy=0, state_chute=0)
                        else:
                            return render_template('index.html', **templateData, state_bpm=0, state_oxy=0, state_chute=1)
                    elif 95 <= oxy <= 100:
                        if chute == 0:
                            return render_template('index.html', **templateData, state_bpm=0, state_oxy=1, state_chute=0)
                        else:
                            return render_template('index.html', **templateData, state_bpm=0, state_oxy=1, state_chute=1)
                    elif 80 <= oxy < 95:
                        if chute == 0:
                            return render_template('index.html', **templateData, state_bpm=0, state_oxy=2, state_chute=0)
                        else:
                            return render_template('index.html', **templateData, state_bpm=0, state_oxy=2, state_chute=1)
                elif 60 <= bpm <= 80:
                    if oxy < 80:
                        if chute == 0:
                            return render_template('index.html', **templateData, state_bpm=1, state_oxy=0, state_chute=0)
                        else:
                            return render_template('index.html', **templateData, state_bpm=1, state_oxy=0, state_chute=1)
                    elif 95 <= oxy <= 100:
                        if chute == 0:
                            return render_template('index.html', **templateData, state_bpm=1, state_oxy=1, state_chute=0)
                        else:
                            return render_template('index.html', **templateData, state_bpm=1, state_oxy=1, state_chute=1)
                    elif 80 <= oxy < 95:
                        if chute == 0:
                            return render_template('index.html', **templateData, state_bpm=1, state_oxy=2, state_chute=0)
                        else:
                            return render_template('index.html', **templateData, state_bpm=1, state_oxy=2, state_chute=1)
                elif 40 <= bpm < 60 or 80 < bpm <= 100:
                    if oxy < 80:
                        if chute == 0:
                            return render_template('index.html', **templateData, state_bpm=2, state_oxy=0, state_chute=0)
                        else:
                            return render_template('index.html', **templateData, state_bpm=2, state_oxy=0, state_chute=1)
                    elif 95 <= oxy <= 100:
                        if chute == 0:
                            return render_template('index.html', **templateData, state_bpm=2, state_oxy=1, state_chute=0)
                        else:
                            return render_template('index.html', **templateData, state_bpm=2, state_oxy=1, state_chute=1)
                    elif 80 <= oxy < 95:
                        if chute == 0:
                            return render_template('index.html', **templateData, state_bpm=2, state_oxy=2, state_chute=0)
                        else:
                            return render_template('index.html', **templateData, state_bpm=2, state_oxy=2, state_chute=1)


@app.route("/login", methods=['POST'])
def login():
    POST_NAME = str(request.form['nom'])
    POST_PRENOM = str(request.form['prenom'])
    POST_PASSWORD = str(request.form['password'])
    var = "SELECT id, nom, prenom, password, privilege FROM users WHERE nom = '" + POST_NAME + "' AND password = '" + POST_PASSWORD + "' AND prenom = '" + POST_PRENOM + "'"
    try:
        cur = bdd_login()
    except pymysql.Error as e:
        return session_out(e)
    result = cur.execute(var)
    cur.close()
    for row in cur:
        print(row)
    if result:
        session.permanent = True
        session['logged_in'] = True
        session['prenom'] = POST_PRENOM
        session['nom'] = POST_NAME
        session['ID'] = row[0]
        if row[4] == 'patient':
            session['rank'] = 0
        if row[4] == 'proche':
            session['rank'] = 1
        if row[4] == 'med':
            session['rank'] = 2
        if row[4] == 'admin':
            session['rank'] = 3
        app.NOM = session['nom']
        app.PRENOM = session['prenom']
        app.RANK = session['rank']
        app.ID = session['ID']
        templateData = {'prenom': app.PRENOM, 'nom': app.NOM, 'rank': app.RANK}
        return render_template('index.html', **templateData)
    else:
        return render_template('login.html', login=0)


@app.route("/logout")
def logout():
    session['logged_in'] = False
    session.pop('prenom', None)
    session.pop('nom', None)
    session.pop('ID', None)
    session.pop('rank', None)
    app.PRENOM = ""
    app.NOM = ""
    app.RANK = ""
    app.ID = ""
    return index()

1 Ответ

0 голосов
/ 14 февраля 2020

если я правильно понял, вы имеете дело с нижеуказанными проблемами

  1. После входа отображается страница index, но URL не меняется.
  2. после выхода из системы выдается ошибка.

в основном вы визуализируете index.html шаблон внутри login маршрута, вы должны перенаправить после успешного запроса входа в систему.

вы не можете напрямую вызвать index() внутри logout трактуйте их как маршруты, отличные от нормальных функций

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...