首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不知道我做错了什么

我不知道我做错了什么
EN

Stack Overflow用户
提问于 2022-12-01 20:00:10
回答 1查看 48关注 0票数 -2

我试图调用类AQIparameters来向用户显示存储在函数aqi_parameters中的变量,但它只是在am参数中显示字符串。

我尝试在函数中调用aqiparameters类,但是它会导致位置错误或其他形式的错误。

类和函数所在的form.py

代码语言:javascript
复制
代码语言:javascript
复制
from AQI_WebApp_Flask import main_functions
import requests
from flask_wtf import FlaskForm
from wtforms import SelectField
import folium

class AQIParameters(FlaskForm):
    aqiparameter = SelectField('aqiparameter', choices=[("coordinates","coordinates"),
                                                      ("pr","pr"),
                                                      ("hu", "hu"),
                                                      ("aqius", "aqius")])


def aqi_parameter():
    url = "https://api.airvisual.com/v2/nearest_city?key="
    aqi_key = main_functions.read_from_file("AQI_WebApp_Flask/JSON_Files/aqi_key.json")
    aqi_key = aqi_key['aqi_key']

    url2 = url+aqi_key

    request_json = requests.get(url2).json()

    main_functions.save_to_file(request_json, "AQI_WebApp_Flask/JSON_Files/aqi.json")

    air_quality_index = main_functions.read_from_file("AQI_WebApp_Flask/JSON_Files/aqi.json")


    '''
    From the json file read above, please find the right values for the variables below
    Notice that I already accessed the values for latitude and longitude
    and I also concatenated them into one variable called coordinates
    '''

    latitude = air_quality_index['data']['location']['coordinates'][0]
    longitude = air_quality_index['data']['location']['coordinates'][1]
    coordinates = str(latitude)+', '+str(longitude)

    temperatureC = air_quality_index['data']['current']['weather']['tp']
    pressure = air_quality_index['data']['current']['weather']['pr']
    humidity = air_quality_index['data']['current']['weather']['hu']
    aqius = air_quality_index['data']['current']['pollution']['aqius']


    parameters = {'coordinates': coordinates,
                  'temperatureC': str(temperatureC),
                  'pressure': str(pressure),
                  'humidity': str(humidity),
                  'aqius': str(aqius)}


    return parameters
代码语言:javascript
复制

用户交互主要发生的routes.py

代码语言:javascript
复制
from AQI_WebApp_Flask import app, forms
from flask import request, render_template


@app.route('/', methods=['GET', 'POST'])
def search():
    searchForm = forms.AQIParameters(request.form)
    if request.method == "POST":

        parameters_requested = request.form['aqiparameter']
        aqi_parameter = forms.aqi_parameter()

        '''
        If the user makes a post request, please save the selected value by the user into a variable
        Also, call the function aqi_parameter (from forms.py) with all the results
        Then, render template parameter_result.html only with the parameter requested by the user
        Which means that you should assign the correct value for the variable parameter_requested below
        '''

        return render_template('parameter_result.html', result=parameters_requested, aqi_parameter=aqi_parameter)
    return render_template('parameter_search.html', form=searchForm)
EN

回答 1

Stack Overflow用户

发布于 2022-12-01 20:19:03

看起来,您遇到的问题是,当您尝试访问模板中的aqi_parameter字典的值时,您使用的是字典键的字符串名称,而不是键本身。

例如,在模板中,您试图使用aqi_parameter.coordinates访问坐标值,但这将无法工作,因为坐标不是aqi_parameter对象的属性。相反,您需要使用方括号符号来访问字典的值,如下所示:aqi_parameter‘坐标’。

下面是一个如何在模板中访问aqi_parameter字典值的示例:

代码语言:javascript
复制
<p>Coordinates: {{ aqi_parameter['coordinates'] }}</p>
<p>Temperature: {{ aqi_parameter['temperatureC'] }}</p>
<p>Pressure: {{ aqi_parameter['pressure'] }}</p>
<p>Humidity: {{ aqi_parameter['humidity'] }}</p>
<p>AQI: {{ aqi_parameter['aqius'] }}</p>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74647548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档