On the authentication and login _python in Django

Source: Internet
Author: User
Tags auth

Certified Login

There are a number of ways in Django.contrib.auth, and here are three of them:

1 Authenticate (**credentials) provides user authentication, which verifies that the username and password are correct

Typically requires username password two keyword parameters

If the authentication information is valid, a User object is returned. Authenticate () sets a property on the user object identifying the authenticated backend that authenticated the user, and that information is required in the subsequent login process. When we try to log on to a user object that is extracted directly from the database without authenticate (), it will be an error!!

user = Authentica (username= ' Someone ', password= ' Somepassword ')

2 Login (HttpRequest, user, Backend=none)

The function accepts a HttpRequest object and an authenticated user object

This function uses the Django session framework to attach information such as the sessions ID to an authenticated user.

From Django.contrib.auth import Authenticate, login

def my_view (request):
  username = Request. post[' username ']
  password = Request. post[' password ']
  user = Authenticate (username=username, Password=password)
  if user is not None:
    Login ( Request, user)
    # Redirect to a success page.
    ...
  else:
    # Return of ' invalid login ' error message.
    ...

3 Logout (Request) Log off user

From Django.contrib.auth import logout

def logout_view (request):
  logout (Request)
  # Redirect to a Success page.

The function accepts a HttpRequest object with no return value.

When the function is called, the session information for the current request is cleared

This user does not use this function even if he is not logged in

allow only logged-in users to access

If you want to:

1 Users can access certain pages after landing,

2 Jump to the login page if the user does not have a login to access the page

3 users in the jump landing interface completed after landing, automatic access to the previous access to the address

We have several ways to achieve this:

1 roughness

Detection request.user.is_authenticated ()

From django.conf import settings to
django.shortcuts import redirect

def my_view (request):
  if not Request.user.is_authenticated (): Return
    redirect ('%s?next=%s '% (settings). Login_url, Request.path))

2 The login_required function in Django

Django has designed an adorner for this situation: Login_requierd ()

From django.contrib.auth.decorators import login_required 
   
@login_required 
def my_view (Request): 
  

Logon verification is required before running the My_view function.

1 If the user does not log in, they will jump to the Django default login URL '/accounts/login/' (this value can be modified by Login_url in the settings file). and passes the absolute path of the current access URL (when the login succeeds, it redirects to the path).

You can use the Login_url parameter to configure the login URL.

You can use the Redirect_field_name parameter to configure the absolute path of the current access URL.

If you want to use the Django default login interface, you can do so by configuring it in urls.py so that if you are not logged in, the program defaults to

"templates\registration\login.html" this template.

#urls. py ...
(R ' ^accounts/login/$ ', ' django.contrib.auth.views.login '),

2 If the user logged in, it will enter the normal page

The above is small series for everyone to talk about Django certification and login all content, I hope that we support cloud Habitat Community ~

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.