def get(self, *args, **kwargs): error_summary = "" merchant_id = self.get_argument("nid", None) if not merchant_id: crumbs = "添加商户" form = MerchantForm() method = "POST" else: crumbs = "编辑商户" form = MerchantForm() #根据ID获取用户信息 service = MerchantService() detail = service.fetch_merchant_detail_by_nid(merchant_id) country_caption = detail.pop("country_caption") country_id = detail.get("country_id") form.country_id.widget.choices.append({ "value": country_id, "text": country_caption }) method = "put" form.init_value(detail) self.render("Merchant/merchantEdit.html", form=form, crumbs=crumbs, method=method, summary=error_summary, nid=merchant_id)
def put(self, *args, **kwargs): """修改商户""" error_summary = "" form = MerchantForm() merchant_id = self.get_argument("nid", None) try: is_valid = form.valid(self) if is_valid: if form._value_dict["country_id"] == "0": form._error_dict["country_id"] = "请选择县(区)ID" else: nid = form._value_dict.pop("nid") del form._value_dict["city_id"] del form._value_dict["province_id"] # 添加到数据库 service = MerchantService() db_result = service.update_merchant( nid, **form._value_dict) print(db_result) if db_result: self.redirect("merchantManager.html") return else: error_summary = "更新失败" else: form.init_value(form._value_dict) except Exception as e: error_summary = str(e) service = MerchantService() detail = service.fetch_merchant_detail_by_nid(merchant_id) country_caption = detail.pop("country_caption") country_id = detail.get("country_id") form.country_id.widget.choices.append({ "value": country_id, "text": country_caption }) self.render("Merchant/merchantEdit.html", form=form, crumbs="编辑商户", method="put", summary=error_summary, nid=merchant_id)