Python学习:Django开发_02
??JSON?????API???????JSON????????????? CSRF???????????CSRF??? ??????????????????????? ????????????????????????
发布日期:2021-04-30 21:05:25
浏览次数:138
分类:精选文章
本文共 4611 字,大约阅读时间需要 15 分钟。
Django?????????API??
?????????
???????????????????????????Django??????
python manage.py runserver 0.0.0.0:80
??????????????
python manage.py makemigrations commonpython manage.py migrate
??????
API????
????????mgr/urls.py??????
from django.urls import pathfrom mgr import customerurlpatterns = [ path('customers/', customer.dispatcher),] ????????
??????
??customer.py??listcustomers???
from django.http import JsonResponsefrom common.models import Customerdef listcustomers(request): qs = Customer.objects.values() return JsonResponse({ 'ret': 0, 'retlist': list(qs) }) ????
??addcustomer???
def addcustomer(request): info = request.params['data'] Customer.objects.create( name=info['name'], phonenumber=info['phonenumber'], address=info['address'] ) return JsonResponse({ 'ret': 0, 'id': Customer.objects.last().id }) ??????
??modifycustomer???
def modifycustomer(request): customerid = request.params['id'] newdata = request.params['newdata'] try: customer = Customer.objects.get(id=customerid) except Customer.DoesNotExist: return JsonResponse({ 'ret': 1, 'msg': f'ID?{customerid}??????' }) if 'name' in newdata: customer.name = newdata['name'] if 'phonenumber' in newdata: customer.phonenumber = newdata['phonenumber'] if 'address' in newdata: customer.address = newdata['address'] customer.save() return JsonResponse({ 'ret': 0 }) ????
??deletecustomer???
def deletecustomer(request): customerid = request.params['id'] try: customer = Customer.objects.get(id=customerid) except Customer.DoesNotExist: return JsonResponse({ 'ret': 1, 'msg': f'ID?{customerid}??????' }) customer.delete() return JsonResponse({ 'ret': 0 }) ????
????
??sign_in.py??signin???
from django.contrib.auth import authenticate, loginfrom django.http import JsonResponsedef signin(request): username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username, password=password) if user is not None: if user.is_active and user.is_superuser: login(request, user) request.session['usertype'] = 'mgr' return JsonResponse({ 'ret': 0 }) else: return JsonResponse({ 'ret': 1, 'msg': '??????????' }) return JsonResponse({ 'ret': 1, 'msg': '????????' }) ????
??sign_out.py??signout???
from django.contrib.auth import logoutfrom django.http import JsonResponsedef signout(request): logout(request) return JsonResponse({ 'ret': 0 }) ????????
????
?customer.py?dispatcher??????????
from django.http import JsonResponseimport jsondef dispatcher(request): if request.method == 'GET': request.params = request.GET elif request.method in ['POST', 'PUT', 'DELETE']: request.params = json.loads(request.body) action = request.params['action'] if 'usertype' not in request.session: return JsonResponse({ 'ret': 302, 'msg': '???', 'redirect': '/mgr/sign.html' }, status=302) if request.session['usertype'] != 'mgr': return JsonResponse({ 'ret': 302, 'msg': '???mgr??', 'redirect': '/mgr/sign.html' }, status=302) if action == 'list_customer': return listcustomers(request) elif action == 'add_customer': return addcustomer(request) elif action == 'modify_customer': return modifycustomer(request) elif action == 'del_customer': return deletecustomer(request) else: return JsonResponse({ 'ret': 1, 'msg': '??????http??' }) ???????
??????
?bysms/urls.py??????????
from django.conf.urls.static import staticurlpatterns = [ path('admin/', admin.site.urls), path('sales/', include('sales.urls')), path('api/mgr/', include('mgr.urls')),] + static(document_root='./z_dist') ?????
??requests???API?
import requestsimport pprint# ????????response = requests.get('http://localhost/api/mgr/customers?action=list_customer')pprint.pprint(response.json()) ????????
??????????????????Django??API???????????????
发表评论
最新留言
关注你微信了!
[***.104.42.241]2026年05月26日 12时45分11秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!