【愚公系列】2022年01月 Django商城项目17-用户中心-地址管理功能实现(增删改查)

举报
愚公搬代码 发表于 2022/01/24 00:59:15 2022/01/24
【摘要】 前言在商城设计中,用户的收获地址管理是必不可少的功能,而且一个用户还存在多地址的情况,和默认地址。此篇文章就是讲解用户和地址管理功能实现 一、后端逻辑 1.增def post(self,request): # 一个人最多添加20个地址 # 0 先判断当前的用户的地址是否多余等于20个 # 获取当前用户的地址的数量 count = Ad...

前言

在商城设计中,用户的收获地址管理是必不可少的功能,而且一个用户还存在多地址的情况,和默认地址。此篇文章就是讲解用户和地址管理功能实现

一、后端逻辑

1.增

def post(self,request):

        # 一个人最多添加20个地址
        # 0 先判断当前的用户的地址是否多余等于20个
        # 获取当前用户的地址的数量
        count = Address.objects.filter(user=request.user).count()

        count = request.user.addresses.all().count()

        if count >= 20:
            return http.JsonResponse({'code':RETCODE.THROTTLINGERR,'errmsg':'地址超过上限'})


        # 1.接收数据 -- 收件人,地址,,,,邮箱,固定电话,手机号
        json_dict = json.loads(request.body.decode())
        receiver = json_dict.get('receiver')
        province_id = json_dict.get('province_id')
        city_id = json_dict.get('city_id')
        district_id = json_dict.get('district_id')
        place = json_dict.get('place')
        mobile = json_dict.get('mobile')
        tel = json_dict.get('tel')
        email = json_dict.get('email')
        # 2.验证数据
        #         验证邮箱,固定电话,手机号 等
        if not all([receiver, province_id, city_id, district_id, place, mobile]):
            return http.HttpResponseBadRequest('缺少必传参数')
        if not re.match(r'^1[3-9]\d{9}$', mobile):
            return http.HttpResponseBadRequest('参数mobile有误')
        if tel:
            if not re.match(r'^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$', tel):
                return http.HttpResponseBadRequest('参数tel有误')
        if email:
            if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email):
                return http.HttpResponseBadRequest('参数email有误')
        # 3.数据入库
        try:
            # address = Address()
            # address.save()

            address = Address.objects.create(
                user=request.user,
                title=receiver,
                receiver=receiver,
                province_id=province_id,
                city_id=city_id,
                district_id=district_id,
                place=place,
                mobile=mobile,
                tel=tel,
                email=email
            )
        except Exception as e:
            logger.error(e)

        #如果当前用户没有默认地址就给它设置一个默认地址
        if not request.user.default_address:
            request.user.default_address=address
            request.user.save()

        address_dict = {
            "id": address.id,
            "title": address.title,
            "receiver": address.receiver,
            "province": address.province.name,
            "city": address.city.name,
            "district": address.district.name,
            "place": address.place,
            "mobile": address.mobile,
            "tel": address.tel,
            "email": address.email
        }
        # 4.返回相应
        #     返回JSON数据
        return http.JsonResponse({"code":RETCODE.OK,'errmsg':'ok','address':address_dict})

2.删

def delete(self,request,address_id):
        # 1.获取删除哪条数据(id)
        # 2.查询数据库
        try:
            address = Address.objects.get(id=address_id)
        except Address.DoesNotExist:
            return http.JsonResponse({'code':RETCODE.NODATAERR,'errmsg':'暂无此数据'})
        # 3.删除数据
        # address.delete() 物理删除
        try:
            address.is_deleted=True # 逻辑删除
            address.save()
        except Exception as e:
            logger.error(e)
            return http.JsonResponse({'code':RETCODE.DBERR,'errmsg':'删除失败'})
        # 4.返回相应
        return http.JsonResponse({'code':RETCODE.OK,'errmsg':'ok'})

3.改

def put(self,request,address_id):
        # 1.接收前端提交的修改数据
        json_dict = json.loads(request.body.decode())
        receiver = json_dict.get('receiver')
        province_id = json_dict.get('province_id')
        city_id = json_dict.get('city_id')
        district_id = json_dict.get('district_id')
        place = json_dict.get('place')
        mobile = json_dict.get('mobile')
        tel = json_dict.get('tel')
        email = json_dict.get('email')

        # 校验参数
        if not all([receiver, province_id, city_id, district_id, place, mobile]):
            return http.HttpResponseBadRequest('缺少必传参数')
        if not re.match(r'^1[3-9]\d{9}$', mobile):
            return http.HttpResponseBadRequest('参数mobile有误')
        if tel:
            if not re.match(r'^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$', tel):
                return http.HttpResponseBadRequest('参数tel有误')
        if email:
            if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email):
                return http.HttpResponseBadRequest('参数email有误')

        #     3.获取修改哪条数据(id)
        #    4.根据id查询数据
        # address = Address.objects.get(id=address_id)
        # #    5.更新(修改)数据
        # address.receiver=receiver
        # address.mobile=mobile
        # address.save()

        try:
            Address.objects.filter(id=address_id).update(
                user=request.user,
                title=receiver,
                receiver=receiver,
                province_id=province_id,
                city_id=city_id,
                district_id=district_id,
                place=place,
                mobile=mobile,
                tel=tel,
                email=email
            )
        except Exception as e:
            logger.error(e)
            return http.JsonResponse({'code':RETCODE.DBERR,'errmsg':'数据更新失败'})

        #    6.返回相应
        address = Address.objects.get(id=address_id)
        address_dict = {
            "id": address.id,
            "title": address.title,
            "receiver": address.receiver,
            "province": address.province.name,
            "city": address.city.name,
            "district": address.district.name,
            "place": address.place,
            "mobile": address.mobile,
            "tel": address.tel,
            "email": address.email
        }

        return http.JsonResponse({'code':RETCODE.OK,'errmsg':'ok','address':address_dict})

4.查

def get(self,request):

        # 1.根据条件查询信息
        addresses=Address.objects.filter(user=request.user,is_deleted=False)

        # 2.如果需要我们将对象列表转换为字典列表
        addresses_list = []
        for address in addresses:
            addresses_list.append({
                "id": address.id,
                "title": address.title,
                "receiver": address.receiver,
                "province": address.province.name,
                "province_id": address.province_id,
                "city": address.city.name,
                "city_id": address.city_id,
                "district": address.district.name,
                "district_id": address.district_id,
                "place": address.place,
                "mobile": address.mobile,
                "tel": address.tel,
                "email": address.email
            })
        # 3.返回相应
        context = {
            'addresses':addresses_list,
            'default_address_id':request.user.default_address_id
        }
        return render(request,'user_center_site.html',context)

相关django后端逻辑,前端页面设计就不多说

二、实现效果

在这里插入图片描述

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。