gpt4 book ai didi

python - 如何将 python 后端与 flask 和 html&css 连接起来

转载 作者:行者123 更新时间:2023-12-04 15:17:22 26 4
gpt4 key购买 nike

'''应用.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 获取表单信息(搜索)到后端(城市名称),然后再到 flask (城市名称)
如果尝试获取它,我可以从后端获取消息,但我无法将 HTML 表单获取到后端进行处理我面临的问题是我无法将表单数据从我的 HTML 文件获取到我的后端进行处理基本上,我需要后端的城市名称来获取我的天气描述

最佳答案

简答:

因为您的表单提交使用了 get 请求,您可以使用 request.args 来获取查询字符串 ( see also) 的解析内容:

cityname = request.args.get("city_name")

长答案:

我相信您要求的不仅仅是这段代码。我采用了您提供的代码并在线添加了缺失的部分(请不要对生产代码执行此操作)并将 cityname 传递给 render_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>

通过 flask run 启动应用程序允许我在表单字段中输入城市名称并查看结果(例如“柏林”):

rendered result of dress.html for city_name=Berlin

为了显示所选城市的天气描述,您可以创建一个接受城市名称并从网络检索信息的函数(只是一个粗略的草图):

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

weather_for_city 的结果中提取相关数据,并将其传递给 render_template,就像您对其他变量所做的那样。

关于python - 如何将 python 后端与 flask 和 html&css 连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64070037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com