如何将python后端与flask和html连接&;css
''' app.py ''' ''' 天气预报如何将python后端与flask和html连接&;css,python,html,css,flask,Python,Html,Css,Flask,''' app.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=" +
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文件
'''
了解天气,根据天气穿衣服
搜寻
'''
我需要从HTML获取表单信息(搜索)到后端(城市名称),然后再到烧瓶(城市名称)如果尝试获取消息,我可以从后端获取消息,但我无法将HTML表单获取到后端进行处理 我面临的问题是无法将表单数据从HTML文件获取到后端进行处理
基本上,我需要将cityname添加到后端以获取天气描述。简短回答: 因为表单提交使用get请求,所以可以使用
request.args
获取查询字符串()的解析内容:
长答案:
我相信你要求的不仅仅是这段代码。我接受了您提供的代码,并在线添加了缺少的部分(请不要对生产代码执行此操作),还将cityname
传递到render\u template
:
import logging
from datetime import datetime
from flask import render_template, request
from app import app, forms
@app.route("/")
def index():
return render_template("index.html")
@app.route("/dress")
def dress():
cityname = request.args.get("city_name")
# missing in example code
def temperature_condition():
return 'temp cond'
# missing in example code
def clothes():
return 'clothes'
feels_temperature = 'feels temp' # missing in example code
weather_description = 'weather desc' # missing in example code
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, cityname=cityname) # also pass cityname
我创建了一个简约的dress.html
:
<html>
<body>
<p>message = {{ message }}</p>
<p>temp = {{ temp }}</p>
<p>feels_temperature = {{ feels_temperature }}</p>
<p>weather_description = {{ weather_description }}</p>
<p>cityname = {{ cityname }}</p>
</body>
</html>
从城市的
weather\u结果中提取相关数据
,并将其传递到render\u模板
,就像对其他变量所做的那样。你好,Harshit soni,欢迎使用SO!几乎很棒的第一个问题,只是一点指导,让它变得更好(然后得到更好的答案):试着更详细一点。你到底想达到什么目的?您是如何尝试解决您的问题的?您是否收到任何错误消息?如果是,你能包括这些吗?如果你对你的问题付出额外的努力,那么答案会很快出现——而且有额外的价值!看看这个,这个你好,安德鲁,谢谢你的反馈,我试着让它尽可能容易理解,因为我现在可以你好,阿基布,谢谢你的例子,我做了一些改变
cityname = request.args.get("city_name")
import logging
from datetime import datetime
from flask import render_template, request
from app import app, forms
@app.route("/")
def index():
return render_template("index.html")
@app.route("/dress")
def dress():
cityname = request.args.get("city_name")
# missing in example code
def temperature_condition():
return 'temp cond'
# missing in example code
def clothes():
return 'clothes'
feels_temperature = 'feels temp' # missing in example code
weather_description = 'weather desc' # missing in example code
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, cityname=cityname) # also pass cityname
<html>
<body>
<p>message = {{ message }}</p>
<p>temp = {{ temp }}</p>
<p>feels_temperature = {{ feels_temperature }}</p>
<p>weather_description = {{ weather_description }}</p>
<p>cityname = {{ cityname }}</p>
</body>
</html>
import requests, json
import weatherMappingMessage
from app import dress
from keys import *
def weather_for_city(city_name):
base_url = "http://api.openweathermap.org/data/2.5/weather?"
complete_url = base_url + "appid=" + api_key + "&q=" + city_name + "&units=metric"
response = requests.get(complete_url)
if response.status_code == 200:
return response.json() # assumes your API returns a JSON response
else:
# perform some error handling here, maybe apply a retry strategy
pass