Simply use the django authentication system based on the registration landing

Simply use the Django authentication system

User objects

User object is the core authentication system. They usually means your site with user interaction, and to enable restricted access to registered users of information and related content creator

 

The default user's basic properties are:

Create a User object in django

The most direct way to create users is to use create_user () helper:

The following code is inactive accounts

from django.contrib.auth.models import User
user = User.objects.create_user(username, email, password)

user.is_active = 0

user.save()
# If the activation field value to 1 put is_active

 

 

 

 

Certification of a given set of user name and password.

Use the authenticate () , a certification given set of user name and password. It receives keyword arguments in the form of documents, using the default configuration parameters are username and password, if the password is able to match a given user name, it returns a User object. If the password is invalid, the authenticate () returns None.

# Import authenticate ()

 

from django.contrib.auth import authenticate

the User = the authenticate (username = 'john', password = 'Secret')
 IF the User IS  not None:
     # username password is correct 
    IF user.is_active:
         # user has activated 
        # records the user's activation status of 
        the Login (Request, the User)
         # achieve page jump 
        the Response = redirect (Reverse ( ' the user: the user ' ))
         # determine whether the need to remember the username 

        Remember = request.POST.get ( ' Remember ' )
         iF Remember == ' ON ' :
             #Remember username 
            response.set_cookie ( ' username ' , username, max_age = 7 * 21 * 3600 )
         the else :
            response.delete_cookie ( ' username ' )
         return the Response
     the else :
         # the user does not activate 
        return the render (Request, ' login.html ' , { ' errmsg ' : ' Activate your account ' })
 the else :
     # username or password error 
    return the render (Request, ' the login.html ' , { ' ErrMsg ' : ' username or password ' })

 

 

 

 

change Password

Django will not be stored in the user model of the original (plain text) password, but only a hash (complete details, see the document: How Password Management ). For this reason, do not attempt to directly manipulate user's password property. To use the helper function that's why when creating a user.

To modify a user's password, you have several options:

manage.py changepassword * username * provides a method for modifying the command line User passwords. It prompts you to modify a given user's password, you must enter twice. If they match, the new password will be modified immediately. If you do not provide the user, the command line to try to modify the current system user password matches the user name.

You can also change the password by the program, using set_password () :

code show as below:

>>> from django.contrib.auth.models import User

>>> u = User.objects.get(username=‘john‘)

>>> u.set_password(‘new password‘)

>>> u.save()


  

Ps: If you installed Django admin, you can also authentication system admin page modify user passwords.

Reference links:

  http://www.mamicode.com/info-detail-2069023.html

Guess you like

Origin www.cnblogs.com/pythonyeyu/p/11319742.html
Recommended