Flask 中的重定向按钮给出 jinja2.exceptions.UndefinedError: 'app' 未定义

问题描述 投票:0回答:1

我正在构建一个测试网站来熟悉 Flask,我试图通过锚点将用户重定向到登录或注册页面,但是当我进入该页面时,我收到错误

jinja2.exceptions.UndefinedError: 'app' is undefined

这是 HTML:

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>Armored Core 6: Fires of Rubicon</title>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
    <link href="../static/style.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div class="container">
        <div class="logo"> 
            <img src="../static/ac6img.jpg" height="140px" width="140px">
        </div>
        <div class="text"><h1 class="title">Armored Core 6: Fires of Rubicon Fan Page</h1></div>
        <div class="registerbuttondiv"><a href="{{url_for(app.login)}}" ><h1>Register</h1></a></div>
        <div class="loginbuttondiv"><button class="loginbutton" onclick="location.href = 'login.html';"><h1>Login</h1></button></div>        
    </div>
</body>

这是代码

from flask import url_for, Flask, render_template, request


app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')
@app.route('/login', methods=['POST', 'GET'])
def login():
    error = None
    if request.method == 'POST':
        if valid_login(request.form['username'],
                       request.form['password']):
            return log_the_user_in(request.form['username'])
        else:
            error = 'Invalid username/password'
    return render_template('login.html', error=error)


def valid_login(uname, pwd):
    suname = "user"
    spwd = "pwd"
    if uname == suname and pwd == spwd:
        return True
    else:
        return False
    
def log_the_user_in(uname):    
    return render_template('user.html')

这是回溯

File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 1488, in __call__
return self.wsgi_app(environ, start_response)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 1466, in wsgi_app
response = self.handle_exception(e)
           ^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 1463, in wsgi_app
response = self.full_dispatch_request()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 872, in full_dispatch_request
rv = self.handle_user_exception(e)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 870, in full_dispatch_request
rv = self.dispatch_request()
     ^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 855, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/py/hello.py", line 13, in index
return render_template('index.html')
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/templating.py", line 150, in render_template
return _render(app, template, context)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/flask/templating.py", line 131, in _render
rv = template.render(context)
     ^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/py/templates/index.html", line 14, in top-level template code
<div class="registerbuttondiv"><a href="{{url_for(app.login)}}" ><h1>Register</h1></a></div>
File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 485, in getattr
return getattr(obj, attribute)
       ^^^^^^^^^^^^^^^^^^^^^^^
python html flask jinja2
1个回答
0
投票

Jinja 假设 app 是一个你应该从你的路由传递给它的变量,应用程序对象不会自然地持久化到前端,因此

href="{{url_for(app.login)}}"
引用
app
在你的模板上不存在,
href="{{url_for(‘login’)}}"
是正如 @EAW

所提到的,这不是使用 Flask 构建 url 的正常方法
© www.soinside.com 2019 - 2024. All rights reserved.