我在运行代码时遇到以下错误:
" ValueError:查看功能未返回响应"
以下是我的Flask Code:
from flask import Flask, render_template
from flask_socketio import SocketIO, send
from flask import render_template
app = Flask(__name__)
app.config['Secret_Key'] = 'mysecret'
socketio = SocketIO(app)
@app.route('/')
def hello_world():
render_template('index.html')
@socketio.on('message')
def handleMessage(msg):
print ('Message:' + msg)
send (msg, broadcast = True)
if __name__ == '__main__':
socketio.run(app)
以下是我的index.html代码:
<html>
<head>
<title>Chat Room</title>
<script type= "text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script>
</head>
<body>
<script type= "text/javascript">
$(document).ready(function() {
var socket = io.connect('http://127.0.0.1:5000');
socket.on('connect',function() {
send.send('User has connected!');
});
socket.on('message', function(msg) {
$("#messages").append('<li>'+msg+'</li>')
});
$('#sendbutton').on('click',function() {
socket.send($('#myMessage').val());
$('#myMessage').val('');
});
});
</script>
<ul id= "messages"> </ul>
<input type="text" id="myMessage">
<button id="sendbutton">Send</button>
</body>
</html>
当我通过浏览器打开localhost:5000时出现以下错误:
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.
答案 0 :(得分:1)
问题在于:
@app.route('/') def hello_world(): render_template('index.html')
您应该返回渲染的模板,更改为:
@app.route('/')
def hello_world():
return render_template('index.html')
另一个功能可能有同样的问题。