【发布时间】:2021-01-12 03:19:57
【问题描述】:
''' 应用程序.py
from flask import Flask, render_template, request
from weather_backend import temperature_condition,clothes,feels_temperature,weather_description
app = Flask(__name__)
app.config["SECRET_KEY"] = "Secret-key"
@app.route("/")
def index():
return render_template("index.html")
@app.route("/dress")
def dress():
cityname = request.form.get("city_name")
temp = str(temperature_condition())
message = str(clothes())
feels = feels_temperature
description= weather_description
return render_template("dress.html", message=message, temp=temp, feels_temperature=feels,
weather_description=description )
if __name__ == "__main__":
app.run(debug=True)
''' ''' weather_backend.py
import requests, json
import weatherMappingMessage
from app import dress
from keys import *
base_url = "http://api.openweathermap.org/data/2.5/weather?"
city_name =
complete_url = base_url + "appid=" + api_key + "&q=" + city_name + "&units=metric"
response = requests.get(complete_url)
''' HTML 文件 '''
<body>
<div class="head">
<form action= "{{ url_for('dress') }}" class="form" method="GET">
<h1>Get Weather and Dresses according to the Weather</h1>
<div class = "form-box">
<input type="text" class="search-field location" name= "city_name" placeholder="Location...">
<button class="search-btn" type="button">Search</button>
</div>
</form>
</div>
</body>
'''
我需要将表单信息(搜索)从 HTML 获取到后端(城市名称),然后到烧瓶(城市名称)
如果尝试获取它,我可以从后端收到一条消息,但我无法将 HTML 表单发送到后端进行处理
我面临的问题是我无法将表单数据从我的 HTML 文件获取到我的后端进行处理
基本上,我需要后端的城市名称来获取我的天气描述
【问题讨论】:
-
你好 Harshit soni,欢迎来到 SO!几乎很棒的第一个问题,只是一点指导使它变得更好(然后得到更好的答案):尝试更详细一点。你到底想达到什么目的?您如何尝试解决您的问题?您收到任何错误消息吗?如果是,你能包括那些吗?如果您为您的问题付出额外的努力,那么答案将很快出现 - 并且具有额外的价值!
-
你好,Andrew 谢谢你的反馈,我尽量让它更容易理解
-
你好,Akib 谢谢你的例子,我做了一些改变