为什么我不能从flask应用程序控制台记录数据?我在用jinja

2024-03-29 10:31:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将数据加载到我的flask页面中,并将其记录到控制台日志中,但是我不断得到下面的错误

VM165:1 Uncaught ReferenceError: data is not defined
    at <anonymous>:1:13

这些是my.py和.html中的代码片段

import os
from flask import Flask, render_template, jsonify, request, redirect
import json

app = Flask(__name__)

@app.route("/")
def home():
    json_file = open("/Users/-------/Documents/GitHub/Geospatial-Opportunities-for-Supermarkets/supermarket_locations/longos_locations.json", "r", encoding ="utf-8")
    data = json.load(json_file)
    json_file.close
    return render_template("index.html", data = data)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Supermarkets in Toronto</title>

  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.0-rc.3/dist/leaflet.css" />
  <link rel="stylesheet" type="text/css" href="static/css/style.css">
  <script> var data = '{{data}}'; </script>
</head>

Tags: nameimportjsonappflaskdatahtmltemplate