【发布时间】:2021-10-12 07:00:25
【问题描述】:
我是初学者,需要一点支持。 在应用程序中一切正常,但在表单中输入“new york”时,url 中缺少“_”。
我的代码是
def index(request):
if request.method == 'POST':
city = request.POST['city']
if city == '':
return redirect('index')
res = urllib.request.urlopen(f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid=1b79ea7b1251b76ccda81d6bd').read()
json_data = json.loads(res)
datas = {
"country_code" : str(json_data['sys']['country']),
"cordinate": str(json_data['coord']['lon'])+ ''+
str(json_data['coord']['lat']),
"temp": str(int(json_data['main']['temp']-273)) + 'C',
"pressure" : str(json_data['main']['pressure']),
"humidity" : str(json_data['main']['humidity']),
}
作为响应收到“
InvalidURL at /
URL can't contain control characters. '/data/2.5/weather?q=bielsko biala&appid=1b79ea7b1251b76cc0b41a81d6bd' (found at least ' ')
我正在互联网上寻找解决方案,但我自己无法解决。非常感谢大家的帮助
【问题讨论】:
-
让我们试试 new+york?
-
你的代码有错别字,res = res = urllib...
-
我已经更正了谢谢