22

我正在阅读 Miguel Grinberg 的 Flask Mega 教程,但我无法弄清楚为什么现在无法加载索引页面。这是回溯:

  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask_login.py", line 658, in decorated_view
    return func(*args, **kwargs)
  File "/home/asdoylejr/microblog/app/views.py", line 44, in index
    posts = posts)
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template
    context, ctx.app)
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/asdoylejr/microblog/app/templates/index.html", line 2, in top-level template code
    {% extends "base.html" %}
  File "/home/asdoylejr/microblog/app/templates/base.html", line 30, in top-level template code
    {% block content %}{% endblock %}
  File "/home/asdoylejr/microblog/app/templates/index.html", line 7, in block "content"
    {{form.hidden_tag()}}
  File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/jinja2/environment.py", line 397, in getattr
    return getattr(obj, attribute)
UndefinedError: 'form' is undefined

以下是相关索引页面的代码:

<!-- extend base layout -->
{% extends "base.html" %}

{% block content %}
<h1>Hi, {{g.user.nickname}}!</h1>
<form action="" method="post" name="post">
    {{form.hidden_tag()}}
<table>
    <tr>
        <td>Say something:</td>
        <td>{{form.post(size = 30, maxlength = 140)}}</td>
        <td>
        {% for error in form.errors.post %}
        <span style="color: red;">[{{error}}]</span><br>
        {% endfor %}
        </td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value="Post!"></td>
        <td></td>
    </tr>
</table>
</form>
{% for post in posts %}
<p>
    {{post.author.nickname}} says: <b>{{post.body}}</b>
</p>
{% endfor %}
{% endblock %}

这是视图中的代码:

@app.route('/', methods = ['GET', 'POST'])
@app.route('/index', methods = ['GET', 'POST'])
@login_required
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body = form.post.data, timestamp = datetime.utcnow(), author = g.user)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('index'))
    posts = g.user.followed_posts().all()
    return render_template("index.html",
        title = 'Home',
        user = user,
        posts = posts)

我已经多次阅读教程,并在每节课结束时将我的代码与源代码进行了比较,但我不知道它为什么不起作用。我不确定为什么当它在另一个视图中传递表单时没有返回错误时,它在此视图中传递表单时遇到问题。

谁能指出我正确的方向?

4

3 回答 3

29

您收到的错误消息在堆栈跟踪中进行了说明。具体来说,这里:

File "/home/asdoylejr/microblog/app/templates/index.html", line 7, in block "content"
{{form.hidden_tag()}}

File "/home/asdoylejr/microblog/flask/lib/python2.7/site-packages/jinja2/environment.py", line 397, in getattr
return getattr(obj, attribute)

UndefinedError: 'form' is undefined

错误消息来自 Jinja,表示form未定义。您的模板尝试使用form...

...
{% block content %}
<h1>Hi, {{g.user.nickname}}!</h1>
<form action="" method="post" name="post">
    {{form.hidden_tag()}}
<table>
    ...

...但是您从未将它作为 render_template 方法的一部分传递(您只定义了title,userposts)...

return render_template("index.html",
        title = 'Home',
        user = user,
        posts = posts)
于 2013-10-21T22:45:11.410 回答
11

form您应该作为上下文变量传递。尝试这个:

@app.route('/', methods = ['GET', 'POST'])
@app.route('/index', methods = ['GET', 'POST'])
@login_required
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body = form.post.data, timestamp = datetime.utcnow(), author = g.user)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('index'))
    posts = g.user.followed_posts().all()
    return render_template("index.html",
        title = 'Home',
        user = user,
        posts = posts,
        form = form)
于 2013-10-21T22:44:34.643 回答
-1

您没有在模板中传递“表单”。

因为它是你的 html 模板的一部分。

return render_template("index.html",
        title = 'Home',
        user = user,
        posts = posts,
        form = form) # <<<-------------------- Here.
于 2020-01-11T06:31:53.580 回答