学习烧瓶:jinja2.exceptions.TemplateSyntaxError:预期令牌'name',得到了'。

时间:2020-02-21 07:47:41

标签: python flask jinja2

我正在学习Flask,但是我的代码无法运行,看起来不错。我已用Google搜索了该错误,并发现了各种各样的结果,因此,专门针对expected token 'name', got '.'的其他符号之一。我正在测试404页面,它是从base派生的。

from flask import Flask, render_template                                                                 
from flask_bootstrap import Bootstrap

app = Flask(__name__)
bootstrap = Bootstrap(app)

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'), 500

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

app.add_url_rule('/', 'index', index)

@app.route('/user/<name>')
def user(name):
    return render_template('user.html', name=name)

base.html ...

{% extends "bootstrap/base.html" %}

{% block title %}Flasky{% endblock %}

{% block navbar %}
<div class="navbar navbar-inverse" role="navigation">
        <div class="container">
                <div class="navbar-header">
                        <button type="button" class="navbar-toggle"
                         data-toggle="collapse" data-target=".navbar-collapse">
                                <span class="sr-only">Toggle Navigation</span>
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                                <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" href="/">Flasky</a>
                </div>
                <div class="navbar-collapse collapse">
                        <ul class="nav navbar-nav">
                                <li><a href="/">Home</a></li>
                        </ul>
                </div>
        </div>
</div>
{% endblock %}

{% block content %}
<div class="container">
        {% block.page_content %}{% endblock %}
</div>
{% endblock %}

404.html ...

{% extends "base.html" %}

{% block title %}Flasky - Page Not Found{% endblock %}

{% block page_content %}
<div class="page-header">
        <h1>Not Found!</h1>
</div>
{% endblock %}

当尝试请求一堆随机字母以产生404时,会发生以下情况...

127.0.0.1 - - [21/Feb/2020 18:39:19] "GET /asdfsadf HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/app.py", line 1925, in dispatch_request
    self.raise_routing_exception(req)
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/app.py", line 1907, in raise_routing_exception
    raise request.routing_exception
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/ctx.py", line 350, in match_request
    result = self.url_adapter.match(return_rule=True)
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/werkzeug/routing.py", line 1945, in match
    raise NotFound()
werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/app.py", line 1815, in handle_user_exception
    return self.handle_http_exception(e)
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/app.py", line 1743, in handle_http_exception
    return handler(e)
  File "/home/john/devel/learn/flask/myflasky/hello.py", line 9, in page_not_found
    return render_template('404.html'), 404
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/templating.py", line 140, in render_template
    ctx.app,
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/flask/templating.py", line 120, in _render
    rv = template.render(context)
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "/home/john/devel/learn/flask/myflasky/templates/404.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/home/john/devel/learn/flask/myflasky/venv/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "/home/john/devel/learn/flask/myflasky/templates/base.html", line 29, in template
    {% block.page_content %}{% endblock %}
jinja2.exceptions.TemplateSyntaxError: expected token 'name', got '.'

我不知道如何找到此处报告的错误,并且基本上不了解输出。如果有人可以给我指出此错误的含义的信息,例如在我希望不知道的情况下期望“名称”在哪里,那将是很好的选择。

1 个答案:

答案 0 :(得分:0)

我查看了您的代码,发现的第一件事是您访问的路由“ / asdfsadf”不存在。而且您主要要担心的是,在“ base.html”中,它应该是{%block page_content%}而不是{%block.page_content%}。随便问什么!