为什么会出现类型错误:需要1个位置参数,但是有2个参数?

问题描述:

我的django项目遇到了问题。这是因为以下几点:为什么会出现类型错误:需要1个位置参数,但是有2个参数?

获取结果:__init__() takes 1 positional argument but 2 were given

我的代码:

urls.py

url(r'^_get_weather', views._get_weather, name='_get_weather') 

views.py

def _get_weather(request): 
    r = urllib.request.urlopen('http://api.openweathermap.org/data/2.5/weather?APPID=$API&q=Hongkong') 
    s = r.read().decode('utf-8') 
    j = json.loads(s) 
    temp='Current tempearture: {:.2f}'.format(j['main']['temp'] - 273.15) 
    return HttpRequest(temp) 

您的看法函数应返回HttpResponse而不是HttpRequest

+0

谢谢它的作品! – Eric