def put(self, *args, **kwargs): """ 修改商户 :param args: :param kwargs: :return: """ error_summary = "" form = MerchantForm() merchant_id = self.get_argument('nid', None) try: is_valid = form.valid(self) if is_valid: if form._value_dict['county_id'] == '0': form._error_dict['county_id'] = '请选择县(区)ID' else: nid = form._value_dict.pop('nid') del form._value_dict['city_id'] del form._value_dict['province_id'] # 添加到数据库 service = MerchantService(MerchantRepository()) service.update_merchant_by_kwargs(nid, **form._value_dict) self.redirect('/MerchantManager.html') return else: form.init_value(form._value_dict) except Exception as e: error_summary = str(e) service = MerchantService(MerchantRepository()) detail = service.get_merchant_detail_by_nid(merchant_id) county_caption = detail.pop('county_caption') county_id = detail.get('county_id') form.county_id.widget.choices.append({'value': county_id, 'text': county_caption}) self.render('Admin/Merchant/MerchantEdit.html', form=form, crumbs='编辑商户', method='PUT',summary=error_summary, nid=merchant_id)
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(MerchantRepository()) detail = service.get_merchant_detail_by_nid(merchant_id) county_caption = detail.pop('county_caption') county_id = detail.get('county_id') form.county_id.widget.choices.append({'value': county_id, 'text': county_caption}) method = 'PUT' form.init_value(detail) self.render('Admin/Merchant/MerchantEdit.html', form=form, crumbs=crumbs, method=method, summary=error_summary, nid=merchant_id)