为什么我的python for循环在django中只返回一个值


the django views.py
def quiz_automate(request):
val=automate_data()
# print(val['data'])
get_quiz=Quiz.objects.get(subject='english',exam_type='jamb')
for data in val['data']:
print(data['question'])
d_question=Question(text=data['question'],quiz=get_quiz)
d_question.save()
return HttpResponse('saved')`
the scraped.py function  
def automate_data():
url = 'https://questions.aloc.com.ng/api/v2/m?subject=chemistry'
headers = {'Accept': 'application/json','Content-Type': 'application/json'}
r = requests.get(url, headers=headers)
return r.json()

我试着从一个网站抓取数据,它返回了多个值,但每当我在我的django视图中使用存储在我的postgres数据库,它只是存储一个值,而不是多个值

您的返回语句在for循环中使用选项卡。因此,当它到达该行时,它将退出函数,从而只在第一个循环中保存记录。

希望对你有帮助。

最新更新