標籤:cep false django 事務 count except tom creat git
1.隨機產生驗證碼
# import random# print(random.random()) #0-1的小數# print(random.randint(1,3)) #包括1和3# print("--",random.randrange(1,3)) #不包括1和3 #隨機產生四位驗證碼import randomcheckcode = ‘‘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) #abcdefghijklmnopqrstuvwxyzprint(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 @atomicdef cmd(self): model..... model.....
隨機產生驗證碼及python中的事務