在跟着《Flask Web开发:基于Python的Web应用开发实战》这本书中的例子写程序。
有两个页面,一个是主页面index,一个是user页面
打开主页会出现Internal Server Error,/user/xxx页面显示正常
hello.py
from flask import Flask, render_template
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required
from datetime import datetime
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hack me'
manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
class NameForm(Form):
name = StringField('What is your name?', validators=[Required()])
submit = SubmitField('Submit')
@app.errorhandler(404)
def page_note_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
@app.route('/', methods=['GET', 'POST'])
def index():
name = None
form = NameForm()
if form.validate_on_submit():
name = form.name.data
form.name.data = ''
return render_template('index.html', form=form, name=name, current_time=datetime.utcnow())
@app.route('/user/<name>')
def user(name):
return render_template('user.html', name=name)
if __name__ == '__main__':
manager.run()
index.html
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Flasky{% endblock %}
{% block page_content %}
<p class="page-header">
<h1>Hello ,{% if name %}{{ name }}{% else %}Stranger{% endif %}!</h1>
</p>
{{ wtf.quick_form(form) }}
<p>the local time is {{ moment(current_time).format('LLL') }}</p>
<p>that was {{ moment(current_time).fromnow(refresh=True) }}</p>
{% endblock %}
base.html
{% extends "bootstrap/base.html" %}
{% block title %}Flasky{% endblock %}
<!--引用moment.js-->
{% block scripts %}
{{ super() }}
{{ moment.include_moment() }}
{% endblock %}
{% block head %}
{{ super() }}
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon">
<link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon">
{% endblock %}
{% block navbar %}
<p class="navbar navbar-inverse" role="navigation">
<p class="container">
<p 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>
</p>
<p class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
</ul>
</p>
</p>
</p>
{% endblock %}
{% block content %}
<p class="container">
{% block page_content %}{% endblock %}
</p>
{% endblock %}
吐槽一下segmentfault的编辑器:在编辑模式下排版正常,可是实际显示的样式和编辑模式下的排版不一样。
天蓬老师2017-04-17 14:35:33
500 Internal error, there is a problem somewhere in the program
python
app.run(debug=True) #or app.debug = True
Enable debugging mode and you can see the error message in the terminal.
http://dormousehole.readthedocs.org/en/latest/quickstart.html#debug-mode