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("\n", "") 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_tokenpython
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', # 匿名用戶頻率 } }