Хочу добавлять записи в бд, при этом брать инфу из параметров запроса. Но получаю ошибку TypeError: The view function for 'drivers_func' did not return a valid response. The function either returned None or ended without a return statement.
Не понимаю в чем дело потому что ещё вчера всё работало, а сегодня это, может я где-то мискликнул или что-то случайно удалил, но все попытки сегодня сделать это рабочим не увенчались успехом.
Получаю ошибку при попытке создать новго driver (
127.0.0.1:5000/drivers/driver/?first_name=Maki&las...)
Это часть которая начинается с
elif request.method == 'POST':
driver_items = Driver.query.all()
@app.route('/drivers/driver/', methods=['POST', 'GET'])
def drivers_func():
if request.method == 'GET' and request.args.get('created_at__gte'):
"""
The part which is displaying drivers who were created after 10-11-2021
/drivers/driver/?created_at__gte=10-11-2021
"""
params = request.args.get('created_at__gte')
params_format = params.replace('-', '/')
date = datetime.strptime(params_format, "%d/%m/%Y")
result = []
for items in driver_items:
if items.created_at > date:
result.append({
'driver_id': items.id,
'driver_first_name': items.first_name,
'driver_last_name': items.last_name,
'driver_created_at': items.created_at.strftime("%d/%m/%Y %H:%M:%S"),
'driver_updated_at': items.updated_at.strftime("%d/%m/%Y %H:%M:%S"),
})
else:
return f'No drivers were created after or before {date}'
return jsonify(f'Drivers which were created after {date}: ', result)
elif request.method == 'GET' and request.args.get('created_at__lte'):
"""
The part which is displaying drivers who were created before 16-11-2021
/drivers/driver/?created_at__lte=16-11-2021
"""
params = request.args.get('created_at__lte')
params_format = params.replace('-', '/')
date = datetime.strptime(params_format, "%d/%m/%Y")
result = []
for items in driver_items:
if items.created_at < date:
result.append({
'driver_id': items.id,
'driver_first_name': items.first_name,
'driver_last_name': items.last_name,
'driver_created_at': items.created_at.strftime("%d/%m/%Y %H:%M:%S"),
'driver_updated_at': items.updated_at.strftime("%d/%m/%Y %H:%M:%S"),
})
else:
return f'No drivers were created after or before {date}'
return jsonify(f'Drivers which were created before{date}: ', result)
elif request.method == 'POST':
"""
The part which is creating a driver
/drivers/driver/?first_name=Maki&last_name=Asher
"""
first_name_url = request.args.get('first_name')
last_name_url = request.args.get('last_name')
# insert_readings()
result = []
for items in driver_items:
if items.first_name == first_name_url and items.last_name == last_name_url:
return jsonify('Such driver already exist')
else:
new_driver = models.Driver(first_name=first_name_url, last_name=last_name_url)
db.session.add(new_driver)
db.session.commit()
result.append({
'driver_id': items.id,
'driver_first_name': items.first_name,
'driver_last_name': items.last_name,
'driver_created_at': items.created_at.strftime("%d/%m/%Y %H:%M:%S"),
'driver_updated_at': items.updated_at.strftime("%d/%m/%Y %H:%M:%S"),
})
return jsonify('All drivers: ', result)
И подскажите пожалуйста как сделать проверку на то есть ли такой пользователь. потому что моя не работает.
За ранее спасибо