我有下面的代码app.py在 flask 。
from flask import Flask, render_template, request
from flask_bootstrap import Bootstrap
app = Flask(__name__)
Bootstrap(app)
@app.route('/')
def home():
return render_template("home.html")
if __name__ == '__main__':
app.run()
家在哪里.html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Cheppers DevOps Challeng | Home</title>
</head>
<body>
{% extends "template.html" %}
{% block content %}
{% endblock %}
</body>
</html>
该模板有一个简单的引导代码,可以完美地运行。
我想在我所有的html页面上运行bootstrap,我在那里为导航栏创建了一个模板网页(template.html)
出现以下错误:
File "/Volumes/Data/final/web_cheppers/app.py", line 15, in home
return render_template("home.html")
File "/Volumes/Data/final/web_cheppers/venv/lib/python3.7/site-packages/flask/templating.py", line 140, in render_template
ctx.app,
File "/Volumes/Data/final/web_cheppers/venv/lib/python3.7/site-packages/flask/templating.py", line 120, in _render
rv = template.render(context)
File "/Volumes/Data/final/web_cheppers/venv/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/Volumes/Data/final/web_cheppers/venv/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/Volumes/Data/final/web_cheppers/venv/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Volumes/Data/final/web_cheppers/venv/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/Volumes/Data/final/web_cheppers/templates/home.html", line 8, in top-level template code
{% extends "template.html" %}
File "/Volumes/Data/final/web_cheppers/templates/template.html", line 47, in top-level template code
{% block scripts %}
File "/Volumes/Data/final/web_cheppers/templates/template.html", line 49, in block "scripts"
{{ bootstrap.load_js() }}
File "/Volumes/Data/final/web_cheppers/venv/lib/python3.7/site-packages/jinja2/environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'bootstrap' is undefined
我试过卸载和安装Flask-Bootstrap,但仍然一无所获。
1条答案
按热度按时间5hcedyr01#
我已经找到了问题所在。
很明显,我导入了错误的Flask-Bootstrap库,pycharm无法识别。我找到了一个在1.1.0版本上运行Bootstrap-Flask的库