Может ли кто-нибудь мне помочь?Я следовал этому руководству в приложении Flask Weather, но столкнулся с проблемой, когда попытался добавить в приложение больше функциональности.Я могу добавить города, но не могу удалить их в своем коде.В приведенном ниже коде я написал другую функцию, и у меня есть полное приложение на github, которое вы можете попробовать, чтобы увидеть, что происходит не так.Большое спасибо.https://github.com/lashleykeith/weatherapp
app.py
import requests
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///weather.db'
db = SQLAlchemy(app)
class City(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
new_city = request.form.get('city')
if new_city:
new_city_obj = City(name=new_city)
db.session.add(new_city_obj)
db.session.commit()
cities = City.query.all()
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=271d1234d3f497eed5b1d80a07b3fcd1'
weather_data = []
for city in cities:
r = requests.get(url.format(city.name)).json()
weather = {
'city' : city.name,
'temperature' : r['main']['temp'],
'description' : r['weather'][0]['description'],
'icon' : r['weather'][0]['icon'],
}
weather_data.append(weather)
return render_template('weather.html', weather_data=weather_data)
####################################################
# TRYING TO MAKE A DELETE #
# FUNCTION #
# #
####################################################
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
new_city = request.form.get('dcity')
if new_city:
new_city_obj = City(name=new_city)
db.session.delete(new_city_obj)
db.session.commit()
cities = City.query.all()
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=271d1234d3f497eed5b1d80a07b3fcd1'
weather_data = []
for dcity in cities:
r = requests.get(url.format(dcity.name)).json()
weather = {
'city' : dcity.name,
'temperature' : r['main']['temp'],
'description' : r['weather'][0]['description'],
'icon' : r['weather'][0]['icon'],
}
weather_data.delete(weather)
weather_data.delete(weather)
return render_template('weather.html', weather_data=weather_data)
if __name__ == '__main__':
app.debug = True
app.secret_key = '\xb0\xdb\x92P\x89\x12\xb0j\xfc9%)N\xd5\x8f\xfc\xa3\xcf\xecmn\xb9\xc0\xca'
app.run()
/ templates / weather.html
<!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>What's the weather like?</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.2/css/bulma.css" />
</head>
<body>
<section class="hero is-primary">
<div class="hero-body">
<div class="container">
<h1 class="title">
What's the weather like?
</h1>
</div>
</div>
</section>
<section class="section">
<div class="container">
<div class="columns">
<div class="column is-offset-4 is-4">
<form method="POST">
<div class="field has-addons">
<div class="control is-expanded">
<input class="input" name="city" type="text" placeholder="City Name">
</div>
<div class="control">
<button class="button is-info">
Add City
</button>
</div>
</div>
</form>
</div>
</div>
<br>
<div class="columns">
<div class="column is-offset-4 is-4">
<form method="POST">
<div class="field has-addons">
<div class="control is-expanded">
<input class="input" name="dcity" type="text" placeholder="City Name">
</div>
<div class="control">
<button class="button is-info">
Remove City
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
<section class="section">
<div class="container">
<div class="columns">
<div class="column is-offset-4 is-4">
{% for weather in weather_data %}
<div class="box">
<article class="media">
<div class="media-left">
<figure class="image is-50x50">
<img src="http://openweathermap.org/img/w/{{ weather.icon }}.png" alt="Image">
</figure>
</div>
<div class="media-content">
<div class="content">
<p>
<span class="title">{{ weather.city }}</span>
<br>
<span class="subtitle">{{ weather.temperature }}° F</span>
<br> {{ weather.description }}
</p>
</div>
</div>
</article>
</div>
{% endfor %}
</div>
</div>
</div>
</section>
<footer class="footer">
</footer>
</body>
</html>