当前位置:网站首页>Project login and registration ideas

Project login and registration ideas

2022-07-27 05:38:00 Csdn__ F

redis

Main knowledge points

string Type of addition, deletion, modification and query

purpose : Verification code storage uuid Corresponding to the verification code text

increase : set

Delete del set Set up ex Number of seconds

Change set

check get

list Type of addition, deletion, modification and query

Historical record

increase : lpush rpush

Delete del Clear the entire list lrem key count rem

Change Is not the point You can use delete first , Add

check lrange key 0 -1

set Type of addition, deletion, modification and query

Collection

increase : sadd

Delete del Clear the entire list srem key value

Change There is no need to change , It's ok if you don't delete it

check smembers key

hash Type of addition, deletion, modification and query

The shopping cart

increase : hset key key value

Delete del Clear the entire list hdel key key

Change hset key key value

check hgetall

register

front end

check
  1. stay input above , To bind events and data v-model

    <input @blur="checkUser" v-model="username" />   If you find that input You can't input anything ,v-model What's inside , Not in the data It is defined 
    
    1. @click=“ Function names are not bracketed ” Click event When you submit your registration , When submitting login , Generally, it is bound on the button
    2. @blur=“ Function name ” Loss of focus event Usually in input Bound above . What is losing focus , Is the cursor from input Move it out
  2. stay js Of methods Inside , Define a function , The function name is the function name written in the event

    data(){
    	return {
    		"username":"",  // If you don't write this , you input Enter something that doesn't go in 
    	}
    }
    ,methods:{
    	 Function name (){
    		xxxx  The body of the function 
    		//  Want to get username 
    		var uname = this.username
    		//  send out axios Just ask 
    	}
    }
    
Verification Code
<img :src="img_path" @click="ver_img" />   Colon can't be less , There is a function 

import {
    v4 as uuid4} from 'uuid'

data:{
    
    img_path:"",
},
create(){
    
   this.ver_img() #  After the page is loaded , Do it now  
},
methods:{
    
    ver_img(){
    
        uuid = uuid4() #  Generate a uuid
        this.img_path =  "/user/ver_img/" + uuid + "/"
    }
}
register
  1. to button Button binding click event ,
  2. Implementation function , send out axios request ,post request
  3. post With parameters ,post(url, Parameters )
  4. The parameter is a dictionary format { “username”:this.username,“pwd”:this.password }

Back end

check
  1. Get the data from the front desk get/post

    1. Usually in class , We use get+ Dynamic routing , Parameters are passed by route
    2. get The ginseng , The parameters are directly in url above ,request.query_params.get(),
    3. post The ginseng , stay request.data.get() Inside
    4. Route parameters , When you accept it ,def get(self,request, Parameters ) The parameter name must be the same as The name in the route is the same .
  2. check

    1. Uniqueness check

      1. Go to the database to query this data
      def get(self,request,uname):
      	user_info = User.objects.filter(username=uname).first()
          if user_info:
              #  Return existence 
          else:  #  If there is judgment in the future , You don't have to write it else
              #  Return to available 
      
    2. format checks

      1. Define regular rule = r’’
      2. Regular matching
        1. Import re package import re
        2. matching Return value = re.findall( Regular variables , Variables to match strings )
        3. Determine the return value If it is false , Description does not match
        4. According to whether it matches , Return to the corresponding prompt
Generate verification code
  1. Get the message from the front desk UUID , To put it bluntly, it's a string
  2. Generate a 4 Random number of bits ,random.randint(1000,9999)
  3. Generated random number , Can't be used directly . the reason being that int type , Need to be converted to string type str()
  4. Turn random numbers into pictures captcha
  5. You have to match the verification code with uuid The corresponding relationship exists redis Inside
#  Guide pack 
from captcha.image import ImageCaptcha
from django.http import HttpResopnse

def get(self,request,uuid):
    #  The code is incomplete , Only the key things are written , Don't copy directly 
    #  Instantiate the class of the verification code 
    img = ImageCaptcha()

    #  Generate , Call a generation method of the instantiated object 
    img_byte = img.generate( Random string )  # Parameters passed in , It must be of string type , If not , You need str Change my 

    #  save uuid And random string 
    r = redis.Redis( Parameter omitted , Add according to your own conditions )
    r.set(uuid, Random string ,ex=300)

    #  Returns the binary of the picture , You can't use Response
    return HttpResopnse(img_byte,content_type="image/png") #  This function , yes django Bring it with you 
Verification code verification
  1. Get the parameters passed from the foreground uuid and code
  2. according to uuid Go to redis database , Find the corresponding value code_byte, The return value is one byte type , Need to be converted to string type
    1. link redis redis.Redis(host=“127.0.0.1”,port=6379,password=“123456”)
  3. hold redis Checked up , convert to string type code_str = code_byte.decode() # decode Will drift yellow , Normal phenomenon
  4. Judgment database code_str And user input code Is it consistent
  5. If the same , Verification passed , atypism , Verification failed
register
  1. Get the data from the front desk get/post
    1. request.data.get() obtain post
    2. Dynamic routing obtain get Dynamic routing is defined and you? def get When , perhaps post You need to receive this parameter
    3. Possible mistakes :
      1. The parameter name passed from the foreground is different from the parameter name accepted by your backend , The phenomenon is that after the accepted parameters are printed , Show None, Or it may report an error . A hint of None There is no xx attribute , Or the method . solve : After you get the parameters , Debug it right away .
    4. Add : Error resolution :
      1. 404 error , Find the slash and write it , Or is the routing configured , Or is the word wrong
      2. 500 error , Have you got the parameters , Is the code red 、 yellow . If you can't find it, just do it in a code block return Resopnse()
      3. Logic error returned , For example, the user is not logged in 、 Wrong password , There is something wrong with logical judgment . It is not necessarily to judge that there is a problem with this statement , It may be the value of your judgment , Have you got , Or there is a problem with the type . solve : you are here if Before judging , Put the conditions to be judged print come out , Take a look at .
  2. Check the parameters , Omitted in the lecture . Because when inputting before , It was checked once . It doesn't mean it's unnecessary , Just say , The code is repeated , No longer rewrite .
    1. It's best to judge whether the user name already exists
  3. Write to database
    1. Model .objects.create( Keyword parameters )
  4. Return response
    1. return Response() Pay attention to the import package

Sign in

front end

Back end

  1. Get the data from the front desk , User name and password

  2. According to the user name , Find this data , If this data doesn't exist at all , Just return the wrong user name or password .

  3. If the data is found , Compare the passwords . if username == user_info.password:

    1. If it's not consistent , Just return the wrong user name or password

    2. If the same , Return to login successfully , It is not enough to prompt successful login , Also return the user's information

      1. Basic information of users , There is user Back inside , The front desk display uses . user name 、 cell-phone number . Password cannot be returned

      2. User authentication information , Your login credentials ( Payment receipt ).JWT

        1. Generation is simple , Just one line of code

          # pip install pyjwt
          
          import jwt
          from django.conf import settings
          
          payload = {
                      
              "user_id":user_info.id,
              "user_name":user_info.name,
          }
          token = jwt.encode(payload,settings.SECRET_KEY,al...="HS256") #  This line of code is the focus 
          # payload  It's actually user information , It's better to add one inside user_id
          #  To use settings , To guide the package  from django.conf import settings
          
          return Resopnse({
                      
              "code":"",
              "msg":"",
              "user":payload,
              "token":token,
          })
          
原网站

版权声明
本文为[Csdn__ F]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207270501240187.html