基本烧瓶程序返回500服务器错误 [英] basic flask program returning 500 server error

查看:141
本文介绍了基本烧瓶程序返回500服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在PyCharm中设置一个基本的烧瓶(Python)应用程序

下面是用我已经知道的一些Python修改的基本Hello world应用程序。
我改变了返回'Hello world!' return contacts 来尝试打印出我的字典到空白页面,但我得到一个500服务器错误。

我的基本错误是什么?

 from flask import Flask 
$ b app = Flask(__ name__)

$ b @ app.route('/')
def hello_world():

contacts = dict()
johnSmith = dict()
jasonBourne = dict()
lisbethSalander = dict()

johnSmith ['label'] ='John Smith'
johnSmith ['phone'] ='(222)333-4444'
johnSmith ['email'] ='john @ johnsmith .net'

jasonBourne ['label'] ='Jason Bourne'
jasonBourne ['phone'] ='(333)444-5555'
jasonBourne ['email' ] ='jason@cia.gov'

lisbethSalander ['label'] ='Lisbeth Salander'
lisbethSalander ['phone'] ='(444)555-6666'
lisbethSalander [ 'email'] ='lsalander@gmail.com'

contacts ['1'] = johnSmith
contacts ['2'] = jasonBourne
contacts ['3'] = susanBoom
$ b $打印联系人
返回联系人
#return'Hello world!'

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

错误

<$ p $内部服务器错误
服务器遇到内部错误,无法完成您的请求。服务器被重载或应用程序中有错误

在app.py中发生错误第203行

  try:
request_started.send(self)
rv = self.preprocess_request()
如果rv是None:
rv = self.dispatch_request()
除了例外如e:


解决方案

你必须返回一个Flask认为是视图响应的东西。这包括 render_template jsonify 或自定义 响应 对象。在你的情况下,它看起来像你想返回一个JSON对象,所以使用 jsonify


Trying to setup a basic flask (Python) app in PyCharm

Below is the basic 'Hello world' app modified with some Python I already know. I changed the return 'Hello world!' to return contacts to try and print out my dictionaries onto the blank page, but I'm getting a 500 server error.

What is my basic mistake?

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():

    contacts = dict()
    johnSmith = dict()
    jasonBourne = dict()
    lisbethSalander = dict()

    johnSmith['label'] = 'John Smith'
    johnSmith['phone'] = '(222) 333-4444'
    johnSmith['email'] = 'john@johnsmith.net'

    jasonBourne['label'] = 'Jason Bourne'
    jasonBourne['phone'] = '(333) 444-5555'
    jasonBourne['email'] = 'jason@cia.gov'

    lisbethSalander['label'] = 'Lisbeth Salander'
    lisbethSalander['phone'] = '(444) 555-6666'
    lisbethSalander['email'] = 'lsalander@gmail.com'

    contacts['1'] = johnSmith
    contacts['2'] = jasonBourne
    contacts['3'] = susanBoom

    print contacts
    return contacts
    # return 'Hello world!'

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

Error

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

Looks like error happened in app.py line 203

try:
    request_started.send(self)
    rv = self.preprocess_request()
    if rv is None:
        rv = self.dispatch_request()
except Exception as e:

解决方案

You must return something Flask recognizes as a response from a view. This includes render_template, jsonify, or a custom Response object. In your case it looks like you want to return a JSON object, so use jsonify.

这篇关于基本烧瓶程序返回500服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆