zoukankan      html  css  js  c++  java
  • Django+drf学习过程笔记

    PEP8规范:
    	https://blog.csdn.net/ratsniper/article/details/78954852
    
    使pip在安装包时速度加快--改变镜像源:
    	将国外的镜像源改为国内的,可以加快下载速度:如豆瓣安装django 
    	pip install django -i https://pypi.douban.com/simple

    migrate 只对一个app生成表:migrate app名 对所有app生成表:migrate 坑1:在Task中运行createsuperuser不成功时,需要在终端运行python3 manag.py createsuperuser 导入测试数据 如何在app之外的py文件中写脚本运行,将数据导入,如果直接写是不能在django项目中运行的,所需要做以下操作
        import os,sys
    
        pwd = os.path.dirname(os.path.realpath(__file__))
        sys.path.append(pwd+'../')
    
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "OnlinFarMarket.settings")  # 本行在manage.py或wsgi.py中的
        import django
        django.setup()
        ...  # 此处写需要执行的脚本
    
    
    django官方文档-中文版
        可以查看django的基本结构,对学习django有帮助
    
    在使用CBV时,返回序列化数据(不使用drf)
        '''假设goods是刚刚被查出来的一个queryset对象'''
        1.用json序列化(最原始方式),格式为:
    class GoodsViewset():
      ...  
      for
    good in goods:
        json_list
    = {}     json_list['name'] = good.name     json_list['price'] = good.price ...   return HttpResponse(json.dumps(json_list),content_type='application/json') # content_type 必须传 # 缺点:用json.dumps无法序列化时间类型
      2.使用model_to_dict可以简化添加到字典中的过程
    from django.forms.models import model_to_dict

    class GoodsViewset():
      ...
    for good in goods: json_list = model_to_dict(good) json_list.append(json_list)   return HttpResponse(json.dumps(json_list),content_type='application/json')
      3.使用django中的serialize进行序列化
    from django.core import serializers
    class
    GoodsViewset()
      ...
      json_data = serializers.serialize('json',goods) # 需要指明转换为json格式
    return HttpResponse(json_data,content_type='application/json')
      4.使用JsonResponse返回,可免去content_type
    from django.http import JsonResponse
    class GoodsViewset()
      ...
      json_data = serializers.serialize('json',goods)
    return JsonResponse(json_data,safe=False) # 缺点:django的serialize会将顺序定死
    
    
    drf的token登录(了解)
        官网:https://www.django-rest-framework.org/api-guide/authentication/
        github: https://github.com/encode/django-rest-framework/tree/master
        用法;
    1.配置app
    INSTALLED_APPS = [
      ...
      'rest_framework.authtoken'
    ]
    2.添加rest中间件,主要用来验证用户信息,将user添加到request中
    REST_FRAMEWORK = {
      'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.JSONWebTokenAuthentication',
      ]
    }
    资料:projectsedu.com
    
    JWT使用
       1.安装DRF
            pip install djangorestframework-jwt
        2. 配置
            REST_FRAMEWORK = {
                "DEFAULT_AUTHENTICATION_CLASSES":("rest_framework_jwt.authentication.JSONWebTokenAuthentication",)  # 全局设置的方法,也可在单个视图中设置
            }
            JWT_AUTH = {
                'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=100),  # 设置过期时间
            }
        3.添加url
    from rest-framework_jwt.views import obtain_auth_token
      urlpatterns=[
        url(r'^api-token-auth/',views.obtain_auth_token)  # DRF自带的token认证模式
        url(r'^login/',views.obtain_jwt_token) # jwt的认证接口(路径可自定义) ]
      4.访问登录直接向/login/发送请求即可,携带用户名密码,jwt会返回一个token
      5.下次再访问其他url时,只需要携带token即可,格式为:
            key:Authorization
            value:JWT 231c3a1d131b.3159ef63a4d1cb971a8f58
        #注:使用jwt不需要写登录接口,查看源码可进入 obtain_jwt_token 中
    
    顺便解决下跨域问题
      1. pip3 install django-cors-headers
      2.配置
    # 配置app
    INSTALLED_APPS = [
      ...
      'corsheaders',
      ...
    ]
    # 设置中间件
    MIDDLEWARE = [
      ...
      corsheaders.middleware.CorsMiddleware  # 放在第三个
      ...
    ]
    CORS_ALLOW_CREDENTIALS = True
    CORS_ORIGIN_ALLOW_ALL = True
    CORS_ALLOW_HEADERS = (' * ')
    
    
          
    自定义用户认证
    1.需要先写一个方法,位置随意,最终需要导入到配置中
    class CustomBackend(ModelBackend):
      '''
      自定义用户认证
      '''
      # 需要重写authenticate方法
      def authenticate(self, request, username=None, password=None, **kwargs):
        try:
          user = User.objects.get(Q(username=username)|Q(mobile=username))
          if user.check_password(password):
            return user
        except Exception as e:
      return None
      2.在配置中配置
    # 自定义用户验证配置
    AUTHENTICATION_BACKENDS = (
      'app01.views.CustomBackend',  # 路径为authenticate方法位置
    )
    
    云片网进行短信验证
      1.注册云片网账号
      2.开发者信息认证(审核通过)
      3.新建签名(审核通过)
      4.设置模板(审核通过)
      5.查看api文档,发送短信形式有很多,国内国外,单条多条等,以国内单条为例。
      6.官方文档中面向Python的代码写的比较麻烦,咱们可以自己写
        ### yunpian.py
    import requests
    import json
    
    class YunPian(object):
      def __init__(self,api_key):
        self.api_key = api_key
        self.single_send_url = 'https://sms.yunpian.com/v2/sms/single_send.json '  # 此地址可在api文档里查看
      def send_sms(self,code,mobile):     parmas = {       'apikey':self.api_key,       'mobile':mobile,       'text':'【暮雪生鲜】您的验证码是{code},非本人请可忽略'%code     }     response = requests.post(self.single_send_url,data=parmas)     re_dict = json.loads(response.text)     print(re_dict) if __name__ == '__main__': # 测试用   yun_pian = YunPian('d6c4ddf50ab3131d46a1c662b94e') # 将apikey传入   yun_pian.send_sms('4651','13572551532') # 4651就是验证码,135...为要发送的手机号
            ### views.py
    class SmsCodeViewset(CreateModelMixin,GenericViewSet):
        '''
        短信验证码
        '''
        serializer_class = SmsSerializer
    
        def generate_code(self):
            '''
            生成四位数的验证码
            '''
            seeds = '1234567890'
            random_str = []
            for i in range(4):
                random_str.append(choice(seeds))
            self.code = ''.join(random_str)
    
        def create(self, request, *args, **kwargs):
            '''
            发送验证码
            '''
            serializer = self.get_serializer(data=request.data)
            serializer.is_valid(raise_exception=True)
    
            # 以下为重载内容
            mobile = serializer.validated_data['mobile']
            self.generate_code()  # 调用生成随机验证码
            yun_pian = YunPian(APIKEY)  # 实例化,将settings里配置的APIKEY传入
            print('code:::',self.code,mobile)
    
            sms_status = yun_pian.send_sms(self.code,mobile)  # 接收云片网发来的状态码,0表成功,其他代表错误
            if sms_status['code'] != 0:
                return Response({
                    'mobile':sms_status['msg']
                },status=status.HTTP_400_BAD_REQUEST)
            else:
                code_recod = models.VerifyCode(code=self.code, mobile=mobile)
                code_recod.save()
                return Response({
                    'mobile':mobile
                },status=status.HTTP_201_CREATED)
    7.将本地ip添加到系统设置里的白名单里,否则系统不会让你发送信息的,,本地ip可直接进百度搜索‘本地ip’
    
    支付宝支付
      1.搜索蚂蚁金服,进入蚂蚁金服开放平台->登录
        
      2.需要使用沙箱环境进行开发。开发文档->沙箱环境
        
      3.首次需要生成秘钥。开发文档->签名工具,windows需要下载软件,生成一对公钥和密钥
        
      4.三个密钥:
            应用私钥:需要配置在支付宝中(若在后续使用时遇见验签问题,查看是否配置时出错)
            应用公钥:需要配置在项目中(不可泄露)
            支付宝公钥:在个人信息配置中点击查看支付宝公钥,需要配置在项目中
            
      5.在文档中心->支付API->alipay.trade.pay(统一收单交易支付接口)查看api接口规范,其中最重要的两个参数sign和biz_content
        
      6.已封装好的接口代码,单独建立文件alipay.py
    
    from datetime import datetime
    from Crypto.PublicKey import RSA
    from Crypto.Signature import PKCS1_v1_5
    from Crypto.Hash import SHA256
    from urllib.parse import quote_plus
    from base64 import decodebytes, encodebytes
    import json
    
    class AliPay(object):
        """
        支付宝支付接口(PC端支付接口)
        """
        def __init__(self, appid, app_notify_url, app_private_key_path,
                     alipay_public_key_path, return_url, debug=False):
            self.appid = appid
            self.app_notify_url = app_notify_url
            self.app_private_key_path = app_private_key_path
            self.app_private_key = None
            self.return_url = return_url
            with open(self.app_private_key_path) as fp:
                self.app_private_key = RSA.importKey(fp.read())
    
            self.alipay_public_key_path = alipay_public_key_path
            with open(self.alipay_public_key_path) as fp:
                self.alipay_public_key = RSA.importKey(fp.read())
    
            if debug is True:
                self.__gateway = "https://openapi.alipaydev.com/gateway.do"
            else:
                self.__gateway = "https://openapi.alipay.com/gateway.do"
    
        def direct_pay(self, subject, out_trade_no, total_amount, return_url=None, **kwargs):
            biz_content = {
                "subject": subject,
                "out_trade_no": out_trade_no,
                "total_amount": total_amount,
                "product_code": "FAST_INSTANT_TRADE_PAY",
                # "qr_pay_mode":4
            }
    
            biz_content.update(kwargs)
            data = self.build_body("alipay.trade.page.pay", biz_content, self.return_url)
            return self.sign_data(data)
    
        def build_body(self, method, biz_content, return_url=None):
            data = {
                "app_id": self.appid,
                "method": method,
                "charset": "utf-8",
                "sign_type": "RSA2",
                "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                "version": "1.0",
                "biz_content": biz_content
            }
    
            if return_url is not None:
                data["notify_url"] = self.app_notify_url
                data["return_url"] = self.return_url
    
            return data
    
        def sign_data(self, data):
            data.pop("sign", None)
            # 排序后的字符串
            unsigned_items = self.ordered_data(data)
            unsigned_string = "&".join("{0}={1}".format(k, v) for k, v in unsigned_items)
            sign = self.sign(unsigned_string.encode("utf-8"))
            # ordered_items = self.ordered_data(data)
            quoted_string = "&".join("{0}={1}".format(k, quote_plus(v)) for k, v in unsigned_items)
    
            # 获得最终的订单信息字符串
            signed_string = quoted_string + "&sign=" + quote_plus(sign)
            return signed_string
    
        def ordered_data(self, data):
            complex_keys = []
            for key, value in data.items():
                if isinstance(value, dict):
                    complex_keys.append(key)
    
            # 将字典类型的数据dump出来
            for key in complex_keys:
                data[key] = json.dumps(data[key], separators=(',', ':'))
    
            return sorted([(k, v) for k, v in data.items()])
    
        def sign(self, unsigned_string):
            # 开始计算签名
            key = self.app_private_key
            signer = PKCS1_v1_5.new(key)
            signature = signer.sign(SHA256.new(unsigned_string))
            # base64 编码,转换为unicode表示并移除回车
            sign = encodebytes(signature).decode("utf8").replace("
    ", "")
            return sign
    
        def _verify(self, raw_content, signature):
            # 开始计算签名
            key = self.alipay_public_key
            signer = PKCS1_v1_5.new(key)
            digest = SHA256.new()
            digest.update(raw_content.encode("utf8"))
            if signer.verify(digest, decodebytes(signature.encode("utf8"))):
                return True
            return False
    
        def verify(self, data, signature):
            if "sign_type" in data:
                sign_type = data.pop("sign_type")
            # 排序后的字符串
            unsigned_items = self.ordered_data(data)
            message = "&".join(u"{}={}".format(k, v) for k, v in unsigned_items)
            return self._verify(message, signature)
        7. views中使用
    class PayViewset()
        alipay = AliPay(
            appid="2016346813545",  # appid
            app_notify_url="http://54.28.33.60/page1",  # post请求,用于最后检测是否支付成功(异步)
            app_private_key_path="../KEYS/app-private-2048.txt",
            alipay_public_key_path="../KEYS/alipay-public-2048.txt",  # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥
            return_url="http://54.28.33.60/page2",  # get请求,用于支付宝支付完成后立即跳转的页面(同步)
            debug=True,  # 默认False,(上线时 要改为False)
        )
        url = alipay.direct_pay(
            subject="测试订单",  # 订单名称
            out_trade_no="201702021222",  # 订单号
            total_amount=100  # 支付金额
        )
        re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url)  # 此链接就是支付页面的链接
        return redirect(re_url)
    
    
    class Alipay(APIView):
        def get(self):
            '''
            处理return_url接收到的请求
            :return:
            '''
            # 处理与notify_url请求相似,根据业务需求
            pass
    
        def post(self,request):
            '''
            处理notify_url接收到的请求
            :return:
            '''
            processed_dict = {}  # 定义一个字典,用来存放支付宝发来的信息,后面用起来方便
            for key,value in request.POST.items():
                processed_dict[key] = value
            sign = processed_dict.pop('sign',None)  # 将签名取出并删除掉
            
            # 再次实例化一下
            alipay = AliPay(
                appid="2016346813545",  #
                app_notify_url="http://54.28.33.60/page1",  
                app_private_key_path="../KEYS/app-private-2048.txt",
                alipay_public_key_path="../KEYS/alipay-public-2048.txt",  
                return_url="http://54.28.33.60/page2",  
                debug=True,  # 默认False,
            )
    
            verify_result = alipay.verify(processed_dict,sign)  # verify方法会解析所接收的数据,得到是否支付成功的结果,True or False
            if verify_result is True:
                order_sn = processed_dict.get('out_trade_no', None)
                trade_no = processed_dict.get('trade_no', None)
                trade_status = processed_dict.get('trade_status', None)
    
                '''order_sn,trade_no,trade_status都是支付宝返回的订单状态,我们 就可以根据这些状态进行其他操作,如修改数据库订单状态等......'''
                
                return Response('success')  # 最后记着给支付宝返回一个信息
            else:
                '''如果verify_result为False,则说明可能是其他恶意请求访问,直接忽略就行'''
                pass   
    
    微博第三方登录---单独使用
      1.浏览器搜索微博开放平台,注册开发用户
      2.点击微连接->网站接入->立即接入->创建应用
      3.创建的应用可在我的应用里看到
      4.应用信息中基本信息填写,高级信息中配置回调url
      5.测试信息中添加几个测试账号(名字随便输)
        
      6.开始进行开发,查看文档点击进入OAuth2.0授权认证
      7.下拉接口文档中有几个几口及说明,前两个(OAuth2/authorize,OAuth2/access_token)比较重要,其他都为获取到access_token后的应用,会用到
      8.请求用户授权Token
            点击进入第一个,根据请求参数组合出示例中的url(包含;接口url、app_kay和redirect_url),此url就是第一次请求所用的url,访问后会跳转到微博授权页面,授权成功重定向到回调页面
    def get_auth_url():
        weibo_auth_url = 'https://api.weibo.com/oauth2/authorize'
        redirect_url = 'http://127.0.0.1:8000/Book/'
        auth_url = weibo_auth_url+'?client_id=%s&redirect_uri=%s'%(APP_KEY,redirect_url)
        print(auth_url)

       9.获取授权过的Access Token

        点击进入第二个,根据请求参数组合出一个字典,再使用requests中的post方法访问,所接收到的数据则包含access_token

    def get_access_token(code='ae6753cd5ccd24e4f8d8b27c4fbe0197'):
        access_token_url = 'https://api.weibo.com/oauth2/access_token'
        import requests
        re_dict = requests.post(access_token_url,data={
            'client_id': APP_KEY,
            "client_secret": App_Secret,
            "grant_type": 'authorization_code',
            "code": code,
            "redirect_uri": "http://127.0.0.1:8000/Book/"
        })
        print(re_dict.text)
      10.应用1:根据access_token和uid获取用户信息
            查看文档微博API,其中有用户接口,点进去就可以看到获取用户信息需要的参数
    def get_user_info(access_token='2.00_qyvoF0nGLaD0f6846b55d0ZBUIi',uid='5333299539'):
        user_info_url = 'https://api.weibo.com/2/users/show.json'
        info_url = user_info_url+'?access_token=%s&uid=%s'%(access_token,uid)
        print(info_url)
            get方式访问此url就可以获取到json格式的用户信息数据
    
    微博第三方登录---django集成使用
    https://github.com/python-social-auth/social-app-django
        1.安装模块 pip install social-auth-app-django
      2.配置settings
            添加app:'social_django'
        3.修改myslq的引擎
    'default':{
        ...
        'OPTIONS': {
            'init_command': 'SET default_storage_engine=INNODB',  # django链接mysql默认引擎用的是MyIsam,因为不支持事物和外键,所以换为INNODB,否则会出错
        },
    }
       4. migrate一下,模块中的迁移数据就会更新到数据库中(不用makemigrations)
        5.配置认证
    AUTHENTICATION_BACKENDS = (
        ...
        'social_core.backends.weibo.WeiboOAuth2',  # 配置微博登录
        'social_core.backends.qq.QQOAuth2',  # qq登录
        'social_core.backends.weixin.WeixinOAuth2',  # 微信登录
        'django.contrib.auth.backends.ModelBackend',
    )
        6.配置url
    urlpatterns = patterns(
        ...
        url('', include('social_django.urls', namespace='social'))
    )
        7.配置模板
    TEMPLATES = [
        {
            ...
            'OPTIONS': {
                ...
                'context_processors': [
                    ...
                    'social_django.context_processors.backends',
                    'social_django.context_processors.login_redirect', 
                    ...
                ]
            }
        }
    ]
      8.微博回调地址配置为:http://127.0.0.1/complete/weibo/ 或 将127.0.0.1换为服务器地址(上线时必须要换成服务器地址)
      9.配置appkey和secret
    SOCIAL_AUTH_WEIBO_KEY = 'foobar'  # appkey
    SOCIAL_AUTH_WEIBO_SECRET = 'bazqux'  # secret
    
    # QQ配置
    SOCIAL_AUTH_QQ_KEY = 'foobar'
    SOCIAL_AUTH_QQ_SECRET = 'bazqux'
    
    # 微信配置
    SOCIAL_AUTH_WEIXIN_KEY = 'foobar'
    SOCIAL_AUTH_WEIXIN_SECRET = 'bazqux'
        10.重写do_complete方法
    from social_core.actions import do_complete
    # 此方法返回结果只是跳转到了指定地回调页面,但返回结果中没有用户名和token,前端接收不到则会显示有用户登录
    return backend.strategy.redirect(url)
    改为
        response =  backend.strategy.redirect(url)
        payload = jwt_payload_handler(user)
        response.set_cookie('name',user.name if user.name else user.username,max_age=24*60*60)  #一定要设置过期时间,username就是微博的用户名
        response.set_cookie('token',jwt_encode_handler(payload),max_age=24*60*60)
        return response
        11.然后就可以愉快的将微博登录url设置为 http://127.0.0.1:8000/login/weibo
    
    解释一下为什么支付宝在配置回调url时只能填服务器地址,而微博登录配置的回调地址可以填 127.0.0.1
        1.因为支付宝处理完支付任务后会有两次请求,
            第一次:get请求,支付完成后立即跳转的页面(同步)
            第二次:post请求,用于最后检测是否支付成功(异步)
          因为第二次不是立即发送请求,所以支付宝没法直接给浏览器发指令跳转什么页面
        2.微博不需要发送post请求,所以在测试时可以填写本地地址
    
    缓存:
        # https://django-redis-chs.readthedocs.io/zh_CN/latest/    
        1.配置redis缓存引擎
            1.安装模块
                pip install django-redis
            2.配置引擎
    CACHES = {
        'default':{
            'BACKEND': "django_redis.cache.RedisCache",
            "LOCATION": "redis://127.0.0.1:6379",  
            "OPTIONS": {
                "CLIENT_CLASS": "django_redis.client.DefaultClient",
                "CONNECTION_POOL_KWARGS": {"max_connections": 100},  # 设置连接池最大连接数
                # 'PASSWORD':''
            }
        }
    }
        # http://chibisov.github.io/drf-extensions/docs/
        2. 使用缓存
            1.安装drf扩展模块
                pip install drf-extensions
            2.使用方法1-继承
    # CacheResponseMixin,需要配合ListModelMixin,RetriveModelMixin来使用
    from rest_framework_extensions.cache.mixins import CacheResponseMixin,ListModelMixin,RetriveModelMixin
    class BookViewSet(CacheResponseMixin,ListModelMixin,RetriveModelMixin):  # 将缓存模块放在第一个
        def get(self,request);
            pass
          使用方法2-装饰器
    from rest_framework_extensions.cache.decorators import cache_response
    class BookViewSet(viewsets.ViewSetMixin,APIView):
        @cache_response(timeout=60*60,cache='default')  # timeout缓存时间,cache存储的键名称
        def get(self,request);
            pass
          若使用装饰器时未传参数,也可在settings中进行配置:
    REST_FRAMEWORK_EXTENSIONS = {
        # 缓存时间
        'DEFAULT_CACHE_RESPONSE_TIMEOUT': 60 * 60,
        # 缓存存储
        'DEFAULT_USE_CACHE': 'default',
    }
        3.Session缓存配置
    SESSION_ENGINE = "django.contrib.sessions.backends.cache"
    SESSION_CACHE_ALIAS = "default"

    频率组件
    1. drf自带频率限制配置
    REST_FRAMEWORK={
        # 自带频率全局组件
        'DEFAULT_THROTTLE_classes': (
            'rest_framework.throttling.AnonRateThrottle',  # 匿名用户
            'rest_framework.throttling.UserRateThrottle',  # 已登录用户
        ),
        'DEFAULT_THROTTLE_RATES': {
            'anon': '1 /minute',  # 匿名用户频率
            'user': '3/minute',  # 已登录用户频率
        }
    }
        2.自定义频率限制
            1.自定义频率限制类Mythrottle.py
    from rest_framework.throttling import SimpleRateThrottle
    
    class MyThrottle(SimpleRateThrottle):
        def allow_request(self, request, view):  # 如果想要自己写限制逻辑,就重写此方法,可不写
            if 没有超过限制(伪代码):
                return True
            else:
                return False
        
        scope='rate'  # scope指定settings中所对应的频率的key,依此确定所要限制的频率
        def get_cache_key(self, request, view):  # get_cache_key表示获取所要限制的依据,比如ip
            if request.user.is_authenticated:  # 判断用户是否登录
                if request.user.type == 1:  #判断用户类型
                    return None  # return None表示不对其进行限制
            return request.META.get('REMOTE_ADDR')  # 表示以ip作为限制依据
            2.配置
    REST_FRAMEWORK={
        # 自定义全局组件
        'DEFAULT_THROTTLE_classes': (
            # 'app01.utils.throttle.MyThrottle'
        ),
        'DEFAULT_THROTTLE_RATES': {
            'rate': '1 /minute',  # 匿名用户频率
        }
    }




  • 相关阅读:
    java学习——内部类、匿名内部类
    Java中接口之间的继承
    Settings
    POM
    Maven指令
    Maven生命周期
    内部类
    Modules
    Simple Maven Project
    Maven概述
  • 原文地址:https://www.cnblogs.com/gyk1030/p/11675173.html
Copyright © 2011-2022 走看看