整体目录
模型类models.py
from django.db import models class UrlCoding(models.Model): raw_data = models.CharField(max_length=128, verbose_name='原始数据') coding_count = models.IntegerField(verbose_name='编码次数') new_data = models.CharField(max_length=128, verbose_name='新数据') class JsonDict(models.Model): raw_data = models.TextField(verbose_name='原始数据', default='1') who_change = models.CharField(max_length=32, verbose_name='谁发起的格式转化') new_data = models.TextField(verbose_name='新数据', default='1')
序列化serializers.py
from rest_framework import serializers from apps.tool.models import UrlCoding, JsonDict #ModelSerializer会根据模型自动生成一组字段 class UrlCodingSerializer(serializers.ModelSerializer): class Meta: model = UrlCoding fields = '__all__' #返回数据展示字段为全部 read_only_fields = ('id', 'new_data') #只读字段 class JsonDictSerializer(serializers.ModelSerializer): class Meta: model = JsonDict fields = '__all__' read_only_fields = ('id', 'new_data')
视图views.py
from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView from apps.tool.gj import check_contain_chinese from apps.tool.models import UrlCoding, JsonDict from apps.tool.serializers import UrlCodingSerializer, JsonDictSerializer import urllib.parse class UrlCodingView(APIView): def get(self,request):#request没用到也必须写,不然会报错 url_coding = UrlCoding.objects.all()#获取数据库所有数据 #many=True表示允许有多条数据,instance=url_coding设置要反序列化的数据 us = UrlCodingSerializer(instance=url_coding, many=True) return Response(data=us.data, status=status.HTTP_200_OK) def post(self, request): #序列化是python对象转化为json数据,反序列化是把json数据转为python对象 #反序列化 u = UrlCodingSerializer(data=request.data) #数据验证is_valid(),验证反序列化时的请求参数是否满足序列化器的要求,我这里使用的是ModelSerializer数据库序列化器 #这里的检验要求就是UrlCoding模型类的raw_data 字段的设置就是最大长度不超过128,max_length=128,下面测试我会使用到 if u.is_valid(): raw_data = request.data['raw_data'] coding_count = request.data['coding_count'] #编码次数 if check_contain_chinese(raw_data): new_data = urllib.parse.quote(raw_data) for i in range(coding_count - 1): new_data = urllib.parse.quote(new_data) #ModelSerializer序列化器默认实现了sava保存数据入库的方法,会自动把反序列化的数据存入到数据库 #也可以在里面直接写值 u.save(new_data=new_data) #status状态码,框架自带 return Response(data=u.data, status=status.HTTP_200_OK) return Response({'err': '不包含中文不需要解码'}, status=status.HTTP_400_BAD_REQUEST) return Response(data=u.errors, status=status.HTTP_400_BAD_REQUEST) class JsonDictView(APIView): def get(self,request): json_dict = JsonDict.objects.all() jd = JsonDictSerializer(instance=json_dict, many=True) return Response(data=jd.data, status=status.HTTP_200_OK) def post(self, request): print(request.data) j = JsonDictSerializer(data=request.data) if j.is_valid(): raw_data = request.data['raw_data'] who_change = request.data['who_change'] if who_change == 'json_dict': new_data = raw_data.replace("\"", "'").title().replace('Null', 'None') j.save(new_data=new_data) return Response(data=j.data, status=status.HTTP_200_OK) elif who_change == 'dict_json': new_data = raw_data.replace("'", "\"").lower().replace('none', 'null') j.save(new_data=new_data) return Response(data=j.data, status=status.HTTP_200_OK) return Response({'err': '请填写json_dict或dict_json'}) return Response(data=j.errors, status=status.HTTP_400_BAD_REQUEST)
二级路由apps.tool.urls.py
from django.urls import path, include from apps.tool import views urlpatterns=[ #二级路由 path('url_coding/', views.UrlCodingView.as_view()), path('json_dict',views.JsonDictView.as_view()) ]
一级路由gadget.urls.py
from django.contrib import admin from django.urls import path, include urlpatterns = [ #一级路由 path('admin/', admin.site.urls), path('tool/',include('apps.tool.urls')), ]
测试
长度超过128