Random generation of verification codes and transactions in Python

Source: Internet
Author: User

1. Randomly generated verification code

# import random# print (Random.random ()) #0-1 of decimal # Print (Random.randint (1,3)) #包括1和3 # Print ("--", Random.randrange (1,3)) #不包括1和3 #随机生成四位验证码import Randomcheckcode="' forIinchRange4): current= Random.randrange (0,4)    ifCurrent! =i:temp= Chr (Random.randint ( $, -))    Else: Temp= Random.randint (0,9) Checkcode+=Str (temp) print (checkcode) #KS3G #随机生成8位验证码importstringPrint (string. Ascii_lowercase) #abcdefghijklmnopqrstuvwxyzprint (string. Digits) #0123456789obj= 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. Case one:

Try:     fromdjango.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 ase:response['Status'] =False response['msg'] = str (e)

B. Case two:

#函数里面有数据库操作, add to the function  from django.db.transaction Import Atomic @atomicdef cmd (self):    model ...    .. Model ... ..

Random generation of verification codes and transactions in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.