yeyeyeah1 2024-12-25 21:21 采纳率: 0%
浏览 15

pycharm运行flask,网页打开空白。标黄错误cannot resolve the file

图上是代码,为什么运行flask后,跳转网站显示空白。
app.py中没有任何标黄,index.html中check_login标黄,并显示cannot resolve the file‘check_login"

img

img

@app.route('/')
def hello_world():
    return render_template("index.html")


@app.route('/check_login', methods=['POST'])  # 只接受POST请求
def check_login():
    username = request.values.get("username")
    password = request.values.get("password")
    print(f"UserName:{username} PassWord:{password}")

    # 获取所有用户数据,打印调试
    result = util.find_all()
    print(result)

    # 遍历用户数据,检查用户名和密码
    for user in result:
        if username == user[1] and password == user[2]:
            return redirect("/get_stus")  # 登录成功,跳转到获取学生信息页面
    else:
        return "用户名或密码不存在!请核实!"  # 登录失败,提示错误信息


@app.route("/get_stus", methods=['GET'])  # 只支持GET请求
def get_stus():
    result = util.find_stus()  # 获取学生数据
    print(result)
    context = {"stus": result}
    return render_template("stu-info.html", **context)


@app.route("/delete", methods=['POST', 'GET'])
def delete():
    id = request.values.get("id")  # 获取要删除的学生ID
    util.delete_info(id)  # 删除学生信息
    return redirect("/get_stus")  # 删除后返回学生信息页面


if __name__ == '__main__':
    app.run(debug=True, port=5001)

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
    <!-- 这里如果你不使用JS,可以去掉script标签 -->
    <script type="text/javascript" src=""></script>

    <style>
        #box {
            width: 600px;
            height: 400px;
            border: 5px solid red;
        }
    </style>
</head>
<body>

    <h1>欢迎登陆</h1>

    <!-- 登录表单 -->
    <form action="/check_login" method="post">
        <!-- 用户名输入框 -->
        <label for="username">用户名:</label>
        <input type="text" id="username" name="username" placeholder="请输入用户名" required><br>

        <!-- 密码输入框 -->
        <label for="password">密码:</label>
        <input type="password" id="password" name="password" placeholder="请输入密码" required><br>

        <!-- 提交按钮 -->
        <input type="submit" value="提交">
    </form>

</body>
</html>



  • 写回答

2条回答 默认 最新

  • 关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    根据您提供的信息和代码,我注意到以下几点可能的问题:

    1. 文件路径问题:确保所有文件(如 index.html, util.py, app.py 等)都在正确的位置,并且文件名没有拼写错误。例如,在 app.py 中使用 render_template("index.html") 时,确保 index.html 文件存在于正确的路径下。
    2. 路由问题:确保 Flask 应用中的路由定义正确,并且与 HTML 表单中的 action 属性相匹配。例如,登录表单的 action 是 /check_login,确保 Flask 中有对应的路由处理函数。
    3. 函数名问题:在提供的代码中,有几个函数名出现了不一致的情况,比如 find_allfind_stus,确保这些函数在 util.py 文件中正确定义,并且名称没有拼写错误。
    4. 静态文件路径问题:如果 index.html 中引用了静态文件(如 JavaScript、CSS 文件等),确保这些文件的路径设置正确。例如,<script type="text/javascript" src=""></script> 中的路径可能需要修改为正确的静态文件路径。
    5. 错误提示和重定向问题:在 Flask 中处理登录逻辑时,如果登录成功应该使用 redirect 函数重定向到另一个页面(如获取学生信息的页面),如果登录失败则应该返回相应的错误信息。确保这部分逻辑处理正确。
    6. HTML 表单问题:检查 HTML 表单的 actionmethod 属性是否正确设置,以及表单中的输入字段名称是否与 Flask 代码中获取请求数据的代码相匹配。

    针对您提到的 "cannot resolve the file 'check_login'" 错误,请检查以下几点:

    • 确保 check_login 函数在 app.py 文件中正确定义。
    • 确保 HTML 表单的 action 属性设置为 /check_login
    • 确保 Flask 服务器正在运行,并且您正在访问正确的端口(例如,您可能在代码中指定了 5001 端口)。

    如果以上建议都没有解决问题,请提供更多关于错误的详细信息或截图,以便更准确地诊断问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 12月25日