web - flask页面无法正确显示
本文介绍了web - flask页面无法正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
运行python primer.py后,页面中没有任何内容,只有title能正确显示为'Primer'
以下为文件
primer.py
from flask import Flask, render_template
from flask_wtf import Form
from flask_bootstrap import Bootstrap
from wtforms import StringField, SubmitField
from wtforms.validators import Required, Length
app = Flask(__name__)
bootstrap = Bootstrap(app)
app.config['SECRET_KEY'] = '********'
class MessageFrom(Form):
message = StringField('Please write your message.', \
validators=[Required(), Length(max=140, message='It\'s too long.')])
submit = SubmitField('Submit')
@app.route('/', methods=['GET', 'POST'])
def index():
message = None
form = MessageFrom()
if form.validate_on_submit():
message = form.message.data
form.message.date = ''
return render_template('index.html', form=form, message=message)
if __name__ == '__main__':
app.run(debug=True)
base.html
{% extends "bootstrap/base.html" %}
{% block title %}Primer{% endblock %}
index.html
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Primer{% endblock %}
{% block page_content %}
<div>
<p>weibo:{% if message %}{{ message }}{% else %}There has nothing.{% endif %}</p>
</div>
{{ wtf.quick_form(form) }}
{% endblock %}
解决方案
因为你没有在base.html里面定义page_content块,在
方法1,将index.html中{% block page_content %}
改为{% block body %}
即可
方法2,在base.html的{% block content %} 块里添加page_content块
{% block content %}
{% block page_content %}{% endblock %}
{% endblock %}
content关键字是Bootstrap模版的关键字
page_content的可以是你自定义的
这篇关于web - flask页面无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文