首页 > 解决方案 > 烧瓶不显示电子邮件/密码的值

问题描述

我的 Flask 应用程序遇到了一些问题,而且它似乎没有从我的 SQL 数据库中选择记录。我正在尝试制作一个登录表单,用户在其中输入电子邮件地址和密码 - 然后它从 .db 文件中选择它们都匹配的特定行,然后将这些值作为两个变量返回。没有错误代码,基本功能有效,这意味着没有错误代码。

@app.route("/login", methods=["POST", "GET"])
def login():
  if request.method == "GET":
    return render_template('login.html')
  else:
    session.permanent = True
    email = request.form["email"]
    password = request.form["password"]
    with sqlite3.connect("ToDoList.db") as connection:
      c = connection.cursor()
      c.execute("SELECT * FROM User WHERE email= ? AND password= ? ;", (email, password))
      row = c.fetchone()
      if row:
        session['name'] = row[1]
        session['surname'] = row[2]
        loggedIn = make_response(render_template("todolist.html", name=session["name"], surname=session["surname"]))
        return loggedIn
    resp = make_response(render_template('login.html'))
    return resp

@app.route("/todolist", methods =['GET', 'POST'])
def todolist():
  return render_template("todolist.html")

HTML - 登录

{% extends "base.html" %}
{% block title %} Login Page {% endblock %}

{% block content %}

    {% with messages = get_flashed_messages() %}
        {% if messages %}
            {% for msg in messages %}
                <p>{{msg}}</p>
            {% endfor %}
        {% endif %}
    {% endwith %}

<form action="{{ url_for('todolist')}}" method="post">
    <p>Email: </p>
    <p><input type="email" name="email" /></p>

    <p>Password: </p>
    <p><input type="password" name="password" /></p>

    <p><input type="submit" value ="submit" /></p>
</form>
{% endblock %}

HTML - 待办事项列表

{% extends "base.html" %}
{% block title %} ToDo List {% endblock %}

{% block content %}

    {% with messages = get_flashed_messages() %}
        {% if messages %}
            {% for msg in messages %}
                <p>{{msg}}</p>
            {% endfor %}
        {% endif %}
    {% endwith %}

    <h1>ToDo App</h1>
    <h3>Greetings {{ name }} {{ surname }}</h3>
    Below you can enter in tasks for your ToDo list.

<form action="#" method="POST">
    <h4>Enter the name of a task for your ToDo list: </h4>
    <p><input type="text" name="Task">
    <input type="submit" value="Enter"></p>
</form>

{% endblock %}

我真的只想把名字传到这里,这样他们就会出现——
<h3>Greetings {{ name }} {{ surname }}</h3>

标签: pythonsqliteflask

解决方案


推荐阅读