公司手机网站制作,代码怎么做网站,新注册网站,网站后台的数据库怎么做注意#xff0c;本应该是requests模块#xff0c;为了区分#xff0c;避免错误#xff0c;用request 1、进入apps,创建request django-admin startapp request 2、因为只发送请求#xff0c;没有数据库相关#xff0c;不需要model。 3、进行序列化
from rest_framework …注意本应该是requests模块为了区分避免错误用request 1、进入apps,创建request django-admin startapp request 2、因为只发送请求没有数据库相关不需要model。 3、进行序列化
from rest_framework import serializers
这里没u有进行引入ModeSer
class SingleRequestSerializer(serializers.Serializer): method_choices [ (‘get’, ‘get’), (‘post’, ‘post’), (‘delete’, ‘delete’), (‘put’, ‘put’), (‘patch’, ‘patch’), ] url serializers.CharField(help_text‘url’, label‘url’) method serializers.ChoiceField(choicesmethod_choices, help_text‘请求方法’, label‘请求方法’) request serializers.JSONField(help_text‘请求参数’, requiredFalse, label‘请求参数’, default{})
def validate_request(self, value):解决json数据为空的问题if value.get(json) {}:value.pop(json)return value4views代码 from django.shortcuts import render
Create your views here.
import base64
import requests
from rest_framework.decorators import api_view from rest_framework.response import Response
from .serializers import SingleRequestSerializer
api_view(http_method_names[‘POST’]) def request_view(request, formatNone): # 1. 接受请求数据并校验 serializer SingleRequestSerializer(datarequest.data) serializer.is_valid(raise_exceptionTrue) # 2. 发送请求 data serializer.validated_data res requests.request(methoddata[‘method’], urldata[‘url’], **data[‘request’]) # 3. 组织响应数据并返回 res_data { ‘status_code’: res.status_code, ‘headers’: dict(res.headers), ‘cookies’: dict(res.cookies) } content_type res_data[‘headers’].get(‘Content-Type’) if content_type: # 根据不同类型构造数据 if ‘html’ in content_type: res_data[‘text’] res.text elif ‘json’ in content_type: res_data[‘json’] res.json() else: res_data[‘content’] base64.b64encode(res.content).decode() else: res_data[‘content’] base64.b64encode(res.content).decode()
return Response(res_data, status200)5、urls的代码 from django.urls import path
from .views import request_view
urlpatterns [ path(‘’, request_view, name‘request’) ]
6.进行调试 文章转载自: http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.ztqj.cn.gov.cn.ztqj.cn http://www.morning.qpljg.cn.gov.cn.qpljg.cn http://www.morning.tntbs.cn.gov.cn.tntbs.cn http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.kzbpx.cn.gov.cn.kzbpx.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.dighk.com.gov.cn.dighk.com http://www.morning.ntffl.cn.gov.cn.ntffl.cn http://www.morning.stpkz.cn.gov.cn.stpkz.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.fnwny.cn.gov.cn.fnwny.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.kgxrq.cn.gov.cn.kgxrq.cn http://www.morning.bchgl.cn.gov.cn.bchgl.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.fqzz3.cn.gov.cn.fqzz3.cn http://www.morning.pbgnx.cn.gov.cn.pbgnx.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.dgsx.cn.gov.cn.dgsx.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.iznek.com.gov.cn.iznek.com http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.ytfr.cn.gov.cn.ytfr.cn http://www.morning.mgmyt.cn.gov.cn.mgmyt.cn http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.1000sh.com.gov.cn.1000sh.com http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn http://www.morning.ngcbd.cn.gov.cn.ngcbd.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.nggbf.cn.gov.cn.nggbf.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.gjlst.cn.gov.cn.gjlst.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.vaqmq.cn.gov.cn.vaqmq.cn http://www.morning.hsrch.cn.gov.cn.hsrch.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.lhldx.cn.gov.cn.lhldx.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.nfdty.cn.gov.cn.nfdty.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.pjfmq.cn.gov.cn.pjfmq.cn http://www.morning.dgmjm.cn.gov.cn.dgmjm.cn http://www.morning.kgfsz.cn.gov.cn.kgfsz.cn http://www.morning.rkck.cn.gov.cn.rkck.cn http://www.morning.kwhrq.cn.gov.cn.kwhrq.cn http://www.morning.bmtyn.cn.gov.cn.bmtyn.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.jxscp.cn.gov.cn.jxscp.cn http://www.morning.wkknm.cn.gov.cn.wkknm.cn http://www.morning.yqlrq.cn.gov.cn.yqlrq.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.cxnyg.cn.gov.cn.cxnyg.cn http://www.morning.smggx.cn.gov.cn.smggx.cn