我正在尝试将flask_bootstrap4用于我的flask项目,并复制了flask_bootstrap文档中的代码段,但出现了错误。
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
{% block styles %}
<!-- Bootstrap CSS and Custom css -->
{{ bootstrap.load_css() }}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename=style.css) }}">
{% endblock styles %}
{% if title %}
<title>Community Helper - {{ title }}</title>
{% else %}
<title>Comminity Helper</title>
{% endif %}
{% endblock head %}
</head>
<body>
{% from 'bootstrap/nav.html' import render_nav_item %}
<nav class="navbar navbar-expand-md navbar-light bg-light border">
<div class="navbar-nav mr-auto">
{{ render_nav_item('index', 'Home') }}
{{ render_nav_item('about', 'About') }}
</div>
</nav>
{% block script %}
<!-- Javascript -->
{{ bootstrap.load_js() }}
{% endblock script %}
</body>
</html>
[2019-07-18 15:06:13,553] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/home/energy/.local/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/energy/.local/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/energy/.local/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/energy/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/energy/.local/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/energy/.local/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/energy/Documents/flask-app/comhelper/application.py", line 10, in index
return render_template("index.html")
File "/home/energy/.local/lib/python3.6/site-packages/flask/templating.py", line 140, in render_template
ctx.app,
File "/home/energy/.local/lib/python3.6/site-packages/flask/templating.py", line 120, in _render
rv = template.render(context)
File "/home/energy/.local/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/home/energy/.local/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/home/energy/.local/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/energy/.local/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/home/energy/Documents/flask-app/comhelper/templates/index.html", line 1, in top-level template code
{% extends 'layout.html' %}
File "/home/energy/Documents/flask-app/comhelper/templates/layout.html", line 4, in top-level template code
{% block head %}
File "/home/energy/Documents/flask-app/comhelper/templates/index.html", line 4, in block "head"
{{ super() }}
File "/home/energy/Documents/flask-app/comhelper/templates/layout.html", line 8, in block "head"
{% block styles %}
File "/home/energy/Documents/flask-app/comhelper/templates/layout.html", line 10, in block "styles"
{{ bootstrap.load_css() }}
File "/home/energy/.local/lib/python3.6/site-packages/jinja2/environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'bootstrap' is undefined
127.0.0.1 - - [18/Jul/2019 15:06:13] "GET / HTTP/1.1" 500 -
答案 0 :(得分:0)
我认为您需要包括引导程序。
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
答案 1 :(得分:0)
确定要使用以下命令导入并初始化flask-bootstrap:
from flask_bootstrap import Bootstrap
然后:
Bootstrap(app)
# or bootstrap = Bootstrap(app)
在您的__init__.py中(或在其他初始化库的地方)?