标签:消息 view csdn 配置 create mon lib post import
当表中有大量的foreignkey 外键时,而且后期还有可能加入一些外键时,
使用django自带的contenttype表(表结构是app名和表的对应的每个类字段)
1. Django 中的 content_type
1. 在一对多的表关系中, 添加一条数据,就要去相对应的表中修改,这样做太麻烦,可以考虑用 Content_type 中的主力一对多关系的方法,
例子
from django.db import models # Create your models here. from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation class Electrics(models.Model): name = models.CharField(max_length=32) price = models.IntegerField(default=100) coupons = GenericRelation(to=‘Coupon‘) # 用于反向查询,不会生成表字段 def __str__(self): return self.name class Foods(models.Model): name = models.CharField(max_length=32) price=models.IntegerField(default=100) coupons = GenericRelation(to=‘Coupon‘) def __str__(self): return self.name class Clothes(models.Model): name = models.CharField(max_length=32) price = models.IntegerField(default=100) coupons = GenericRelation(to=‘Coupon‘) def __str__(self): return self.name class bed(models.Model): name = models.CharField(max_length=32) price = models.IntegerField(default=100) coupons = GenericRelation(to=‘Coupon‘) class Coupon(models.Model): name = models.CharField(max_length=32) # 保存的是 contenttype 表中的 id content_type = models.ForeignKey(to=ContentType) # step # 保存的是contenttype 表中的 id 表 中的 对象ID object_id = models.PositiveIntegerField() # step 2 content_object = GenericForeignKey(‘content_type‘, ‘object_id‘) # step 3 def __str__(self): return self.name 应用场景 ‘‘‘ Coupon id name content_type_id object_id 1 美的冰箱满减优惠券 9 3 2 猪蹄买一送一优惠券 10 2 3 床单买一百减五十优惠券 11 1 ‘‘‘
2 . alipay 中python 的接口
pip3 install AliPay 下载一个模块
1, 涉及到公钥和私钥的问题,那么什么是公钥私钥:
1,公钥和私钥成对出现
2,公开的密钥叫公钥,只有自己知道的叫私钥
3,用公钥加密的数据只有对应的私钥可以 解密
4,用私钥加密的数据只有对应的公钥可以解密
5,如果可以用公钥解密,则必然是对应的私钥加的密
6,如果可以用私钥解密,则 必然是对应的公钥加的密
去阿里注册一个 沙箱环境的支付功能,体验一下
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 urllib.parse import urlparse, parse_qs 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)
views 视图中的 代码
from django.shortcuts import render, redirect, HttpResponse from utils.pay import AliPay import json import time def ali(): # 沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info app_id = "2016091100486897" # POST请求,用于最后的检测 notify_url = "http://47.94.172.250:8804/page2/" # notify_url = "http://www.wupeiqi.com:8804/page2/" # GET请求,用于页面的跳转展示 return_url = "http://47.94.172.250:8804/page2/" # return_url = "http://www.wupeiqi.com:8804/page2/" # 本地 私钥文件的位置 merchant_private_key_path = "keys/app_private_2048.txt" # 本地 公钥文件的位置 alipay_public_key_path = "keys/alipay_public_2048.txt" # 调用这个借口类 , 把信息传进去 alipay = AliPay( appid=app_id, app_notify_url=notify_url, return_url=return_url, app_private_key_path=merchant_private_key_path, # 商家私钥(路飞私钥) alipay_public_key_path=alipay_public_key_path, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥 debug=True, # 默认False, 配合沙箱模式使用 ) return alipay def page1(request): if request.method == "GET": return render(request, ‘page1.html‘) else: money = float(request.POST.get(‘money‘)) alipay = ali() # 生成支付的url query_params = alipay.direct_pay( subject="充气式韩红", # 商品简单描述 out_trade_no="x2" + str(time.time()), # 商户订单号 total_amount=money, # 交易金额(单位: 元 保留俩位小数) ) pay_url = "https://openapi.alipaydev.com/gateway.do?{}".format(query_params) return redirect(pay_url) def page2(request): alipay = ali() if request.method == "POST": # 检测是否支付成功 # 去请求体中获取所有返回的数据:状态/订单号 from urllib.parse import parse_qs body_str = request.body.decode(‘utf-8‘) post_data = parse_qs(body_str) post_dict = {} for k, v in post_data.items(): post_dict[k] = v[0] print(post_dict) sign = post_dict.pop(‘sign‘, None) status = alipay.verify(post_dict, sign) print(‘POST验证‘, status) return HttpResponse(‘POST返回‘) else: params = request.GET.dict() sign = params.pop(‘sign‘, None) status = alipay.verify(params, sign) print(‘GET验证‘, status) return HttpResponse(‘支付成功‘)
项目中的 srttings 中的配置
STATIC_URL = ‘/static/‘ STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")] # 支付宝配置参数 ALIPAY_APPID = "你的应用id" ALIPAY_URL = "https://openapi.alipaydev.com/gateway.do"
url 中的 代码
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^page1/‘, views.page1), url(r‘^page2/‘, views.page2), ]
contentType 应用,(表中数据大量存在外键时使用)
标签:消息 view csdn 配置 create mon lib post import
原文地址:https://www.cnblogs.com/zhaoweihang/p/9451236.html