0.页面展示效果

1.后端代码

@api.route("/houses/info", methods=["POST"])
@login_required
def save_house_info():"""保存房屋的基本信息前端发送过来的json数据{"title":"","price":"","area_id":"1","address":"","room_count":"","acreage":"","unit":"","capacity":"","beds":"","deposit":"","min_days":"","max_days":"","facility":["7","8"]}"""# 获取数据user_id = g.user_idhouse_data = request.get_json()title = house_data.get("title")  # 房屋名称标题price = house_data.get("price")  # 房屋单价area_id = house_data.get("area_id")  # 房屋所属城区的编号address = house_data.get("address")  # 房屋地址room_count = house_data.get("room_count")  # 房屋包含的房间数目acreage = house_data.get("acreage")  # 房屋面积unit = house_data.get("unit")  # 房屋布局(几室几厅)capacity = house_data.get("capacity")  # 房屋容纳人数beds = house_data.get("beds")  # 房屋卧床数目deposit = house_data.get("deposit")  # 押金min_days = house_data.get("min_days")  # 最小入住天数max_days = house_data.get("max_days")  # 最大入住天数# 校验参数if not all([title, price, area_id, address, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days]):return jsonify(errno=RET.PARAMERR, errmsg="参数不完整")# 判断金额是否正确try:price = int(float(price) * 100)deposit = int(float(deposit) * 100)except Exception as e:current_app.logger.error(e)return jsonify(errno=RET.PARAMERR, errmsg="参数错误")# 判断城区id是否存在try:area = Area.query.get(area_id)except Exception as e:current_app.logger.error(e)return jsonify(errno=RET.DBERR, errmsg="数据库异常")if area is None:return jsonify(errno=RET.NODATA, errmsg="城区信息有误")# 保存房屋信息house = House(user_id=user_id,area_id=area_id,title=title,price=price,address=address,room_count=room_count,acreage=acreage,unit=unit,capacity=capacity,beds=beds,deposit=deposit,min_days=min_days,max_days=max_days)# 处理房屋的设施信息facility_ids = house_data.get("facility")# 如果用户勾选了设施信息,再保存数据库if facility_ids:# ["7","8"]try:# select  * from ih_facility_info where id in []facilities = Facility.query.filter(Facility.id.in_(facility_ids)).all()except Exception as e:current_app.logger.error(e)return jsonify(errno=RET.DBERR, errmsg="数据库异常")if facilities:# 表示有合法的设施数据# 保存设施数据house.facilities = facilitiestry:db.session.add(house)db.session.commit()except Exception as e:current_app.logger.error(e)db.session.rollback()return jsonify(errno=RET.DBERR, errmsg="保存数据失败")# 保存数据成功return jsonify(errno=RET.OK, errmsg="OK", data={"house_id": house.id})

2.前端html代码

<!DOCTYPE html>
<html>
<head> <meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"><title>爱家-发布新房源</title><link href="/static/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet"><link href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"><link href="/static/css/reset.css" rel="stylesheet"><link href="/static/plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css" rel="stylesheet"><link href="/static/css/ihome/main.css" rel="stylesheet"><link href="/static/css/ihome/newhouse.css" rel="stylesheet">
</head>
<body><div class="container"><div class="top-bar"><div class="nav-bar"><h3 class="page-title">发布新房源</h3><a class="nav-btn fl" href="/myhouse.html"><span><i class="fa fa-angle-left fa-2x"></i></span></a></div></div><div class="houses-con"><ul class="houses-list"><form id="form-house-info"><li><div class="house-title"><h3>基本信息</h3></div><div class="house-content"><div class="form-group"><label for="house-title">房屋标题</label><input type="text" class="form-control" name="title" id="house-title" required></div><div class="form-group"><label for="house-price">每晚价格</label><input type="number" class="form-control" name="price" id="house-price" required></div><div class="form-group"><label for="area-id">所在城区</label><select class="form-control" id="area-id" name="area_id"></select><script type="text/html" id="areas-tmpl">{{ each areas as area}}<option value="{{ area.aid }}">{{ area.aname }}</option>{{ /each }}</script></div><div class="form-group"><label for="house-address">详细地址</label><textarea class="form-control" name="address" id="house-address" required></textarea></div> </div></li><li><div class="house-title"><h3>详细信息</h3></div><div class="house-content"><div class="form-group"><label for="house-room-count">出租房间数目</label><input type="number" class="form-control" name="room_count" id="house-room-count" required></div><div class="form-group"><label for="house-acreage">房屋面积</label><input type="number" class="form-control" name="acreage" id="house-acreage" required></div><div class="form-group"><label for="house-unit">户型描述</label><input type="text" class="form-control" name="unit" id="house-unit" placeholder="如:三室两厅两卫" required></div><div class="form-group"><label for="house-capacity">宜住人数</label><input type="number" class="form-control" name="capacity" id="house-capacity" required></div><div class="form-group"><label for="house-beds">卧床配置</label><input type="text" class="form-control" name="beds" id="house-beds" placeholder="如:双人床:2x1.8x1张 1.5x2x2张" required></div> <div class="form-group"><label for="house-deposit">押金数额</label><input type="number" class="form-control" name="deposit" id="house-deposit" required></div><div class="form-group"><label for="house-min-days">最少入住天数</label><input type="number" class="form-control" name="min_days" id="house-min-days" required></div><div class="form-group"><label for="house-max-days">最多入住天数</label><input type="number" class="form-control" name="max_days" id="house-max-days" placeholder="0表示无限制" required></div></div></li><li><div class="house-title"><h3>配套设施</h3></div><div class="house-content"><ul class="house-facility-list clearfix"><li><div class="checkbox"><label><input type="checkbox" name="facility" value="1">无线网络</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="2">热水淋浴</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="3">空调</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="4">暖气</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="5">允许吸烟</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="6">饮水设备</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="7">牙具</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="8">香皂</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="9">拖鞋</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="10">手纸</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="11">毛巾</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="12">沐浴露、洗发露</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="13">冰箱</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="14">洗衣机</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="15">电梯</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="16">允许做饭</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="17">允许带宠物</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="18">允许聚会</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="19">门禁系统</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="20">停车位</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="21">有线网络</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="22">电视</label></div></li><li><div class="checkbox"><label><input type="checkbox" name="facility" value="23">浴缸</label></div></li></ul></div></li><input type="submit" class="btn btn-success btn-commit" value="发布房源信息"><div class="error-msg text-center"><i class="fa fa-exclamation-circle"></i>请将全部信息填写完整后再提交</div></form><form id="form-house-image" action="/api/house/image" method="post" enctype="multipart/form-data"><input type="hidden" name="house_id" id="house-id" value=""><li><div class="house-title"><h3>添加图片</h3></div><div class="house-content"><div class="house-image-cons"></div><div class="form-group"><label for="house-image">选择图片</label><input type="file" accept="image/*" name="house_image" id="house-image"></div><input type="submit" class="btn btn-success" value="上传"></div></li></form></ul></div><div class="popup_con"><div class="popup"><p><i class="fa fa-spinner fa-spin fa-3x fa-fw"></i></p></div><div class="mask"></div></div><div class="footer"><p><span><i class="fa fa-copyright"></i></span>爱家租房&nbsp;&nbsp;享受家的温馨</p></div> </div><script src="/static/js/jquery.min.js"></script><script src="/static/js/jquery.form.min.js"></script><script src="/static/js/template.js"></script><script src="/static/plugins/bootstrap/js/bootstrap.min.js"></script><script src="/static/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script><script src="/static/plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script><script src="/static/js/ihome/newhouse.js"></script>
</body>
</html>

3.前端js代码

    $("#form-house-info").submit(function (e) {e.preventDefault();// 处理表单数据var data = {};$("#form-house-info").serializeArray().map(function(x) { data[x.name]=x.value });// 收集设置id信息var facility = [];$(":checked[name=facility]").each(function(index, x){facility[index] = $(x).val()});data.facility = facility;// 向后端发送请求$.ajax({url: "/api/v1.0/houses/info",type: "post",contentType: "application/json",data: JSON.stringify(data),dataType: "json",headers: {"X-CSRFToken": getCookie("csrf_token")},success: function (resp) {if (resp.errno == "4101") {// 用户未登录location.href = "/login.html";} else if (resp.errno == "0") {// 隐藏基本信息表单$("#form-house-info").hide();// 显示图片表单$("#form-house-image").show();// 设置图片表单中的house_id$("#house-id").val(resp.data.house_id);} else {alert(resp.errmsg);}}})});

Flask爱家租房--发布新房源(保存房屋基本信息)相关推荐

  1. Flask爱家租房--发布新房源(保存房屋图片)

    0.页面展示效果 1)首先房东填写房屋信息: 2)当房东填写发布的房源信息之后,隐藏(hide)刚才填写信息的界面,同时显示(show)上传房屋图片的界面. 1.后端代码 @api.route(&qu ...

  2. Flask爱家租房--发布新房源(总结)

    重点总结 学习过程中,发现house_id贯穿两个接口内容,现对后端逻辑部分做以下总结: 1)房东首先在前端填写房屋的基本信息,此时通过newhouse.js文件$("#form-house ...

  3. Flask爱家租房--房屋管理(获取房东发布的房源信息条目)

    文章目录 0.效果展示 1.重点总结 2.后端代码 3.前端html 4.前端js 0.效果展示 1.重点总结 1)用户点击"我的房源",页面开始加载,此时myhouse.js限定 ...

  4. flask爱家租房项目开发(一)

    目录 需求文档 创建工程目录以及flask配置 配置日志信息 数据库设计与迁移 静态文件的配置,使浏览器可以正常访问文件 csrf 防护机制 需求文档 需求功能 1. 主页     1.1 最多5个房 ...

  5. Flask爱家租房--房屋管理(搜索房屋列表)

    文章目录 0.效果展示 1.后端接口 2.前端js 3.前端html 0.效果展示 1.后端接口 house.py部分接口: # GET /api/v1.0/houses?sd=2017-12-01& ...

  6. Flask爱家租房--订单支付(支付过程)

    文章目录 0.支付流程 1. 重点总结 2.后端代码 3.前端js 4.前端html 0.支付流程 1. 重点总结 1)用户进入"我的订单"页面,点击"去支付" ...

  7. Flask爱家租房--房屋管理(获取主页幻灯片展示的房屋基本信息)

    文章目录 0.效果展示 1.重点总结 2.后端代码 3.前端js 4.前端html 0.效果展示 1.重点总结 1)当用户访问首页时,开始加载页面信息,此时index.js文件首先调用后端接口chec ...

  8. flask爱家租房项目开发(十三)

    本节文档下载地址:https://download.csdn.net/download/geek_xiong/11615541 目录 蚂蚁金服--支付宝的使用 订单支付 前后端代码编写 测试 蚂蚁金服 ...

  9. Flask爱家租房--房屋管理(获取房屋详情)

    文章目录 0.效果展示 1.思路总结 2.后端接口 3.前端js 4.前端html 0.效果展示 1.思路总结 1)房屋详情页面开始加载时,detail.js首先通过定义的函数(重点:document ...

最新文章

  1. 参加软件测试培训需要学习哪些知识
  2. SAP PM项目导向维护
  3. 预测 “疯狂三月” 冠军的办法,我只告诉你!
  4. Redis 单机模式,主从模式,哨兵模式(sentinel),集群模式(cluster),第三方模式优缺点分析
  5. 巧用云计算 突围移动APP行业乱象
  6. 我国5G有望引领全球 2020年前将商用
  7. Java 删除ArrayList中重复元素,保持顺序
  8. orm设置bool型 python_python基础教程之基本内置数据类型介绍
  9. java5个线程_java基础thread——java5之后的多线程(浅尝辄止)
  10. CentOS 非图形界面用户如何上网
  11. html登陆不刷新flask,Flask Button运行Python而不刷新页面?
  12. day1-接口测试与接口测试工具
  13. 服务器远程桌面一直正在配置,关于远程桌面一直显示正在配置远程会话
  14. TiledMap简单使用
  15. cf鼠标宏数据大全_钛度黑百合电竞定制鼠标,酷炫配置为电竞而生
  16. 【将列表中的每个数据转换成倒数 np.reciprocal()】
  17. 用python+turtle画太阳花
  18. 流控大师 panabit
  19. 自旋锁(spinlock)
  20. 找到读取文本文件的方法

热门文章

  1. 基于GPU的K-Means聚类算法
  2. 阶乘的精确值 大数问题
  3. SQL零基础学习笔记(一)
  4. 解决 springboot + JPA + MySQL 表名全大写 出现 “表不存在” 问题(Table ‘XXX.xxx‘ doesn‘t exist)
  5. Spring中的@scope注解
  6. PLSQL的表窗口开启(不小心把PLSQL的表窗口关了,在哪里打开)
  7. 垃圾回收算法与垃圾回收器
  8. android 去掉标题栏、状态栏、横屏
  9. 透明(颜色)渐变背景(颜色透明背景),兼容IE8
  10. poj 2891 Strange Way to Express Integers