Before and after adding django drf deserialization end simple separation Categories

  • Deserialization class
# Anti-serialized commodity classification 
class CategoryUnSerializer (serializers.Serializer):
     # constraint 
    name = serializers.CharField (MAX_LENGTH = 32 )
     # adding 
    DEF Create (Self, validated_data):
         return models.Category.objects.create (** validated_data)
  • Add Method Categories
from rest_framework.views Import APIView
 from rest_framework.response Import the Response
 from . Import Models
 from serializers. Import * # Categories class CateView (APIView):
      DEF POST (Self, Request): 
        obj = CategoryUnSerializer (the Data = request.data)   # call deserialization IF obj.is_valid ():          # If by constraining 
            obj.save ()               # Create deserialization method performed in return the Response ({
                 '



        
            Status ' : 200 is ,
                 ' MSG ' : '' ,
                 ' Data ' : '' 
            }) 
        return the Response ({
             ' Status ' : 201 ,
             ' MSG ' : ' adding Failed ' ,
             ' Data ' : ' ' 
        })
  • Adding commodity components
<template>
    <div id="addcate">
        <p>分类名称<input type="text" v-model="catename"></p>
        <button @click="sub">添加</button>

    </div>
</template>

<script>
export default {
    name:'addcate',
    data() {
        return {
            catename:''    //初始
            
        }
    },
    methods:{
        sub:function(){
            this.axios({
                url:'/api/app01/cate/',
                method:'post',
                data:{'name':this.catename}
            }).then(res=>{
                if(res.data.status==200){
                    alert('添加成功')
                }else{
                    alert(res.data.msg)
                }
            })
        }
    }

}
  • The component registration to route
import addcate from '@/components/AddCate'


   {
      path: '/addcate',
      name: 'addcate',
      component: addcate
    },
  • effect

Guess you like

Origin www.cnblogs.com/u-damowang1/p/12142922.html