隨機產生驗證碼及python中的事務

來源:互聯網
上載者:User

標籤: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中的事務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.