#背景:最近要写个demo,Django写东西比较快,先拿DJango试一下。
首先建立一个project
django-admin startproject HelloWorld
然后准备虚拟环境
virtualenv env
然后激活
source env/bin/activate
下载个rest_framework
pip install django
pip install djangorestframework
在project里面新建一个app
django-admin startapp app1
app1里面的view改吧改吧
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.
# https://www.cnblogs.com/huchong/p/8450355.html
class TestView(APIView):
def get(self,request):
t_num = request.GET.get("t_num")
print("捕获到参数", t_num)
return Response({"321323":{"132":"321"},"312":[[321,321,431],[321,41,321]]})
然后urls加上去,url里面记得import view
from django.contrib import admin from django.urls import path from app1.views import TestView urlpatterns = [ path('admin/', admin.site.urls), path('hello/', TestView.as_view()), ]
setting里面加上去
INSTALLED_APPS = [ 'rest_framework', ]
更改可以访问的ip为*(我这里是项目测试方便,真实ip你们自己设置哈)
ALLOWED_HOSTS = ['*']
然后就可以用postman测试了。
对了,顺便说一下防火墙开启,我是用8091端口启动的。用ufw就可以了。
sudo ufw allow 8091/tcp