I have just studied python2 for a month, and the basic function of this code has been realized. However, there is such a mess in the view function. How to optimize this code? the following code is mainly through the data submitted by the form, sending post requests to the backend through ajax to modify and add
. < hr >@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()