Django session, user, and registration

Source: Internet
Author: User

1.cookies session (not used yet)

The first HTTP access will send some cookies to the browser, and then each visit to the browser will tell the server what its own cookies are.

2.session session (not used yet)

Django's session framework:

(1) The existence of middleware: ' Django.contrib.sessions.middleware.SessionMiddleware '

(2) "django.contrib.sessions" in Installed_apps

After the session frame is activated, you can use Request.session to be a Dictionary object.

3. Users and authentication

(1)Django.contrib.auth ' put in your installed_apps settings

(2) Confirm   sessionmiddleware   behind   middleware_classes   settings include Django.contrib.auth.middleware.AuthenticationMiddleware '

After installation, you can use the user, listed as Request.user

Authentication () Validation

Login () logon

Logout () Logout

1 defLogin_ (Request):2     """Login"""3Context = {}4     ifRequest.method = ='POST':5form =LoginForm (request, request.) POST)6         ifform.is_valid ():7user = Form.get_user ()#Get User Instance8             ifUser:9 Login (request, user)Ten                 ifForm.get_auto_login ():#Set Session One Request.session.set_expiry (None) A  -                 returnHttpresponseredirect ('/') -context['form'] =form the  -     Else: -form =LoginForm () -context['form'] =form +     returnRender (Request,'login.html', context)

(1) Authenticate Authenticated Users (user)

1  fromDjango.contrib.authImportAuthenticate2user = Authenticate (username='John', password='Secret')3 ifUser is  notNone:4     #Verify successful return of user object5     ifuser.is_active:6         Print("# is_active Verify whether the user is active")7     Else:8         Print("Inactive words can not log in, will not delete the user, only set the flag bit")9 Else:Ten     #validation Failure returns a None One     Print("validation Failed")

(2) Default permissions: Generally, the database operation is to delete and change the search (not required)

Reference Link: http://python.usyiyi.cn/django/topics/auth/default.html

Django session, user, and registration

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.