刚刚学习 python2 个月,这段代码基本功能实现了,不过视图函数里面写这么一堆好乱,请问下如何优化这段代码比较好呢,以下代码主要就是通过表单提交的数据,通过 ajax 发送 post 请求到后端,实现修改和新增
@admin.route("/set", methods=['GET', 'POST'])
def set_task():
if request.method == 'GET':
req = request.args
task_id = int(req.get('id', 0))
task_item = None
if task_id:
task_item = TaskItem.find(task_id)
task_cats = TaskCategory.find_all()
return render_template('admin/task/edit.html', task_item=task_item, task_cats=task_cats)
req = request.values
task_id = req['id'] if 'id' in req else ''
task_name = req['task_name'] if 'task_name' in req else ''
cat_id = int(req['cat_id']) if 'cat_id' in req else ''
price = req['price'] if 'price' in req else ''
quantity = int(req['quantity']) if 'quantity' in req else ''
if task_id:
task_item = TaskItem.find(task_id)
model_task_item = task_item
model_task_item.update_time = get_current_time()
else:
model_task_item = TaskItem()
model_task_item.create_time = get_current_time()
model_task_item.name = task_name
model_task_item.category_id = cat_id
model_task_item.price = price
model_task_item.quantity = quantity
model_task_item.summary = ''
model_task_item.main_image = ''
db.session.add(model_task_item)
db.session.commit()
return Success()
哆啦的时光机
动漫人物
宝慕林4294392
相关分类