我目前的问题是连接前端和后端。我已经在init.py中完成了所有的数据处理,它在文件末尾有一条包含数据的有效json消息。现在,我想将这些数据发布到flask前端,并获取前端中的数据,以便使用这些数据在index.html上可视化某些内容。我不知道如何从这里发帖,因为我尝试过从init.py发帖,但它不起作用
文件结构:
-Application
-app
-templates
-index.html
-__init__.py
-routes.py
-check.py
init.py:
from flask import Flask
import requests
import json
app = Flask(__name__)
from app import routes
#MESSAGE IS A JSON AND HAS DATA
message=json.dumps(dicts)
routes.py:
from flask import render_template
from app import app
@app.route('/')
@app.route('/index')
@app.route('/getjson', methods = ['GET'])
def index():
user = {'username': 'Me'}
return render_template('index.html', title='Home', user=user)
index.html:
<html>
<head>
<title>{{ title }} - Status Checker</title>
</head>
<body>
<h1>Hello, Me!</h1>
</body>
</html>
check.py:
from app import app
将此r = requests.post("http://localhost:5000/getjson", json=message)
添加到init.py会导致错误:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url: /getjson (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x04ED1250>: Failed to
establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
您最好在路由内加载消息,如下所示:
然后,您必须在html中添加一个jinja循环,以显示部分消息结构。如果您需要帮助,请告诉我,添加有关消息的信息以及您希望在html中显示的内容
相关问题 更多 >
编程相关推荐