Django 城市名称中的关键字错误
我在django制作了一个小型天气web应用程序&它工作正常,但是 当输入错误的城市名称时,它开始显示KeyError页面Django 城市名称中的关键字错误,django,openweathermap,Django,Openweathermap,我在django制作了一个小型天气web应用程序&它工作正常,但是 当输入错误的城市名称时,它开始显示KeyError页面 from django.shortcuts import render, redirect from django.contrib import messages import requests #search page def search(request): return render(request, 'index.html') #forecast res
from django.shortcuts import render, redirect
from django.contrib import messages
import requests
#search page
def search(request):
return render(request, 'index.html')
#forecast result page
def forecast(request):
c = request.POST['city']
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid=7fee53226a6fbc936e0308a3f4941aaa&units=metric'.format(c)
r = requests.get(url)
data = r.json()
weather = {
'description': data['weather'][0]['description'],
'icon': data['weather'][0]['icon'],
'city': c.title(),
'temperature': data['main']['temp']
}
print(r)
return render(request, 'weather.html', {'weather': weather})
输入错误的城市名称时,它会给出KeyError,因此我希望django不会给出KeyError,而是将其重定向到我的主页,即index.html,并在其下方显示一条错误消息。API将告诉您城市名称是否无效
r = requests.get(url)
if r.status_code == 404:
messages.add_message('City not found')
return redirect('home')
data = r.json()
...
首先,请不要自己构造QueryString:QueryString不能包含很多字符。您可以使用Django的QueryDict
,例如:
from django.http import QueryDict
qd = QueryDict(mutable=True)
qd.update(q=c, appid='7fee53226a6fbc936e0308a3f4941aaa', units='metric')
url = 'http://api.openweathermap.org/data/2.5/weather?{}'.format(qd.urlencode())
因此,它用+
替换空格
此外,您还可以使用try
-,除了此处的重定向到其他页面,如:
from django.http import QueryDict
from django.shortcuts import redirect
def forecast(request):
try:
city = request.POST['city']
except:
return redirect('name-of-some-view')
qd = QueryDict(mutable=True)
qd.update(q=city, appid='7fee53226a6fbc936e0308a3f4941aaa', units='metric')
url = 'http://api.openweathermap.org/data/2.5/weather?{}'.format(qd.urlencode())
try:
data = r.json()
weather = {
'description': data['weather'][0]['description'],
'icon': data['weather'][0]['icon'],
'city': c.title(),
'temperature': data['main']['temp']
}
except KeyError:
return redirect('name-of-some-view')
return render(request, 'weather.html', {'weather': weather})
从django.http导入QueryDict
从django.shortcuts导入重定向
def预测(请求):
尝试:
城市=请求。发布['city']
除:
返回重定向('name-of-some-view')
qd=QueryDict(可变=True)
qd.update(q=city,appid='7fee53226a6fbc936e0308a3f4941aaa',units='metric')
url='1〕http://api.openweathermap.org/data/2.5/weather?{}.格式(qd.urlencode())
尝试:
data=r.json()
天气={
“说明”:数据['weather'][0]['description'],
“图标”:数据['weather'][0]['icon'],
“城市”:c.标题(),
“温度”:数据['main']['temp']
}
除KeyError外:
返回重定向('name-of-some-view')
返回渲染(请求'weather.html',{'weather':weather})
您可以使用向用户显示消息
from django.http import QueryDict
from django.shortcuts import redirect
def forecast(request):
try:
city = request.POST['city']
except:
return redirect('name-of-some-view')
qd = QueryDict(mutable=True)
qd.update(q=city, appid='7fee53226a6fbc936e0308a3f4941aaa', units='metric')
url = 'http://api.openweathermap.org/data/2.5/weather?{}'.format(qd.urlencode())
try:
data = r.json()
weather = {
'description': data['weather'][0]['description'],
'icon': data['weather'][0]['icon'],
'city': c.title(),
'temperature': data['main']['temp']
}
except KeyError:
return redirect('name-of-some-view')
return render(request, 'weather.html', {'weather': weather})