1.随机生成验证码
# import random # print(random.random()) #0-1的小数 # print(random.randint(1,3)) #包括1和3 # print("--",random.randrange(1,3)) #不包括1和3 #随机生成四位验证码 import random checkcode = ‘‘ for i in range(4): current = random.randrange(0,4) if current != i: temp = chr(random.randint(65,90)) else: temp = random.randint(0,9) checkcode += str(temp) print(checkcode) #KS3G #随机生成8位验证码 import string print(string.ascii_lowercase) #abcdefghijklmnopqrstuvwxyz print(string.digits) #0123456789 obj = random.sample(string.ascii_lowercase+string.digits,8) print(obj) #[‘i‘, ‘m‘, ‘o‘, ‘9‘, ‘6‘, ‘p‘, ‘g‘, ‘0‘] row = "".join(random.sample(string.ascii_lowercase+string.digits,8)) print(row) #417x6kyt
a. 事例一:
try: from django.db import transaction with transaction.atomic(): models.UpDown.objects.create(user_id=user_id,article_id=article_id,up=False) models.Article.objects.filter(nid=article_id).update(down_count=F(‘down_count‘)+1) except Exception as e: response[‘status‘] = False response[‘msg‘] = str(e)
b. 事例二:
#函数里面有数据库操作,加在函数上 from django.db.transaction import atomic @atomic def cmd(self): model..... model.....
时间: 2024-10-20 18:23:59