python - flask 一直显示500 Internal Server Error
PHP中文网
PHP中文网 2017-04-17 14:33:33
0
1
1215

在跟着《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的编辑器:在编辑模式下排版正常,可是实际显示的样式和编辑模式下的排版不一样。

PHP中文网
PHP中文网

认证高级PHP讲师

membalas semua(1)
Peter_Zhu

500内部错误,程序某个地方有问题

pythonapp.run(debug=True)
#or
app.debug = True

打开调试模式,就可以在终端看到错误信息了。

http://dormousehole.readthedocs.org/en/latest/quickstart.html#debug-mode

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan