python flask运行时提示出错了

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

这是app.py的代码,这个运行用的端口号是5500

from flask import Flask, render_template
from livereload import Server

app = Flask(__name__)
@app.route("/")
def index():
    return render_template("index.html", title="<h2>welcome to flask</h2>", body="<h4>hello flask</h4>")

@app.template_filter("md")
def markdown_to_html(txt):
    from markdown import markdown
    return markdown(txt)

def read_md(filename):
    with open(filename) as md_file:
        content = reduce(lambda x, y: x+y, md_file.readlines())
    return content.decode("utf-8")

@app.context_processor
def inject_method():
    return dict(read_md=read_md)

if __name__ == "__main__":
    live_server = Server(app.wsgi_app)
    live_server.watch("**/*.*")
    live_server.serve(open_url=True)

设置app.py debug的提示

图片描述

图片描述

下面这个是flaskblog.py的代码,运行端口号是5000,

from flask import Flask, render_template, request, redirect, url_for, make_response
from werkzeug.routing import BaseConverter
from werkzeug.utils import secure_filename
from flask.ext.script import Manager
from os import path


class RegexConverter(BaseConverter):
    def __init__(self, url_map, *items):
        super(RegexConverter, self).__init__(url_map)
        self.regex = items[0]

app = Flask(__name__)
app.url_map.converters["regex"] = RegexConverter
manager = Manager(app)

@app.route('/')
def hello_world():
    response = make_response(render_template("index.html", content="welcome to flask web development"))
    response.set_cookie("username","")
    return response

@app.route('/service')
def service():
    return 'service'

@app.route('/about')
def about():
    return 'about'

@app.route("/user/<regex('[a-z]{3}'):user_id>")
def user(user_id):
    return "user is %s" % user_id

@app.route("/login",methods=['GET','POST'])
def login():
    if request.method == "post":
        username = request.form["username"]
        password = request.form["password"]
    else:
        username = request.args["username"]
    return render_template("login.html", method=request.method)

@app.route("/upload",methods=["GET","POST"])
def upload():
    if request.method == "post":
        f = request.files("file")
        basepath = path.abspath(path.dirname(__file__))
        upload_path = path.join(basepath, "static/upload")
        f.save(upload_path, secure_filename(f.filename))
        return redirect(url_for("upload"))
    return render_template("upload.html")

@app.errorhandler(404)
def page_note_found(error):
    return render_template("404.html")

@manager.command
def dev():
    from livereload import Server
    live_server = Server(app.wsgi_app)
    live_server.watch("*", "*.*")
    live_server.serve(open_url=True)

if __name__ == '__main__':
    manager.run()
    #app.run()

debug提示如下:
图片描述

阅读 45k
5 个回答

你在 flask app 启动的地方把启动模式改成 debug 试试。

app.run(debug=True)

可以打印出出错原因。

为什么要使用livereload?Flask自带reload机制的,修改代码以后保存就会reload。所以你直接app.run()就可以了。

f.save(path.join(upload_path, secure_filename(f.filename)))
# 好好对比一下我这一代码与那里相应的代码有什么区别,不谢!

新手上路,请多包涵

这是编码问题,你读取的那个文件,前面不要用txt编辑写入,用Pycharm打开,默认保存为utf-8的编码格式,就好了。我刚也遇到这个情况,现在解决了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏