Django's Auth module

Source: Internet
Author: User
Tags aliases

First, let's review how the route aliases are used, just review the route aliases for level two routing how to use

• 1. Use a level two routing alias in the View function, plus the name of the app + ":" + "Routing alias"

From django.urls import Reversereturn redirect (reverse ("App1:auth_index_view"))

  

2. Use the alias of level two route in HTML file

<form action= "{% url ' app1:auth_login_view_name '%}" method= "POST" Novalidate >    {% csrf_token%}    {{ Obj.username.label_tag}}{{Obj.username}}    <span>{{obj.errors.username.0}}</span>    {{ Obj.userpwd.label_tag}}{{Obj.userpwd}}    <span>{{obj.errors.userpwd.0}}</span>{#    <a href= " {% url ' app1:auth_login_view_name '%} "> Point me Big </a>#}    <input type=" Submit "value=" Commit "></form>

  

Below we formally enter the Auth module study

1, first need to import two modules

From Django.contrib Import Auth

  

From django.contrib.auth.models import user# Creating a user's module

  

2, then here is the specific method of use of the Auth module

def auth_login_view (request): Method_type = Request.method.lower () if Method_type = = "Get": Form_obj = my_new _forms () return render (Request, "auth_login.html", {"obj": form_obj}) Else:username = R Equest. Post.get ("username") Userpwd = Request. Post.get ("userpwd") user = Auth.authenticate (username=username,password=userpwd) # This must be user with no other, verify the username and password Whether through validation, if passed returns a Usr object, and if not, returns an anonymous user object if User:auth.login (request,user) # encapsulates username this information into R  Equest.user Print (dir (user)) from django.contrib.auth.models Import user New_common_user  = User.objects.create_user () # Create normal User New_super_user = User.objects.create_superuser () #            Create Super Admin user Error_create_user = User.objects.create () # We don't use this method to create a user, this method creates a user's password is not encrypted, and generally can not be used user = User.objects.get (username= "AAA") User.set_password () user.save () # Change password, here need to save to take effect User.check_password () # When the user needs to change the password, first determine the current input password is correct RET Urn Redirect (reverse ("App1:auth_index_view")) Else:return Redirect (Reverse ("App1:auth_login_view_name") )

  

3, auth module also comes with an adorner, used to determine whether the user login, you also need to import a module first

From django.contrib.auth.decorators import login_required# to determine whether the user is logged in the adorner

  

@login_required # Django's own decorator to determine if a user is logged on to Def auth_index_view (request):    print (request.user.is_authenticated ())    # Determine if a user is logged in, if yes returns true return    render (request, "auth_index.html", {"name": Request.user.username})

  

If the user does not log in successfully, then the page needs to jump to a page, this page needs to be configured in Settings

# If there is no landing land, the default jump to which Urllogin_url = "http://www.baidu.com"

  

4, the method of Auth module cancellation

def logout (Request):    auth.logout (Request)    # Auth.logout method, delete the cookies and seesion    return redirect (Reverse (" App1:auth_login_view_name "))

  

In addition, there are only a limited number of fields in the Django Default User table, and if we want to expand the fields we have, we have 2 ways to deal with them.

Method 1, create a new table, one to the user table

# 1, create a new table, one-to Auth_User table from django.contrib.auth.models import user# First, the Django-brought User table is imported into class User_detail (models. Model):    phone = models. Charfield (max_length=11)    usr_obj = models. Onetoonefield (To=user)    # A one-to-one link to the Django-brought User table

  

Method 2, using the inherited method, it is important to note that if you use the inherited method, the Django database has a auth_user table, so the above method if you use auth_user this table, then only need to modify the table name to indicate that we

# 2, write a class from django.contrib.auth.models import userfrom django.contrib.auth.models import Abstractbaseuserclass Userinfo (abstractbaseuser):    phone = models. Charfield (max_length=11)    addr = models. TextField () # If we use inheritance to extend the built-in Auth_User table, this is the time to configure the default user to authenticate with the table # Auth_user_model = "App1" in settings.py. Userinfo "

  

Django's Auth module

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.