zoukankan      html  css  js  c++  java
  • Django验证模块

    Django验证模块

    • user 对象:

      1. username
      2. password
      3. email
      4. first_name
      5. last_name
    • 常用方法:

      1. create_user(): from django.contrib.auth.models import User user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword') user.save()
      2. 创建超级用户:$ python manage.py createsuperuser -- username=joe --email=joe@example.com
      3. 更改密码: from django.contrib.auth.models import User u = User.objects.get(username='john') u.set_password('new password') u.save()
      4. authenticate(**credentials) 用于验证用户,一般传进去用户名和密码参数,如果通过验证,则返回一个user obeject对象,否则返回None 代码:

        from django.contrib.auth import authenticate
        user = authenticate(username='john', password='secret')
        if user is not None:
        # the password verified for the user
            if user.is_active:
                print("User is valid, active and authenticated")
            else:
                print("The password is valid, but the account has been disabled!")
        else:
            # the authentication system was unable to verify the username and password
        print("The username and password were incorrect.")
        
      5. is_authenticated()

            if request.user.is_authenticated():
                # Do something for authenticated users.
                ...
            else:
                # Do something for anonymous users.
        

      6.登陆验证模块

              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:
                  if user.is_active:
                      login(request, user)
                      # Redirect to a success page.
                  else:
                      # Return a 'disabled account' error message
                      ... 
              else:
                  # Return an 'invalid login' error message.
                  ... 
      

      7.logout()

          from django.contrib.auth import logout
          def logout_view(request):
          logout(request)
          # Redirect to a success page.
      

      8.装饰器

      # login_required 装饰器 不带参数 如果没有登陆,则返回到settings.LOGIN_URL表示的URL
      from django.contrib.auth.decorators import login_required
      @login_required
      def my_view(request):
      ...
      # login_required(login_url='')带login_url参数,没有登陆时就会跳到login_url中
      from django.contrib.auth.decorators import login_required
      @login_required(login_url='/accounts/login/')
      def my_view(request):
          ...
  • 相关阅读:
    Android Studio移动鼠标显示悬浮提示的设置方法
    解决adb push时出现的"Read-only file system"问题
    VIM常见用法总结
    忘记oracle的sys用户密码怎么修改以及Oracle 11g 默认用户名和密码
    Oracle中session和processes的设置
    ssh相关原理学习与常见错误总结
    如何利用Oracle VM Templates 在几分钟内部署Oracle Real Application Clusters (RAC)
    Oracle导入导出常用命令
    Tomcat 到底依赖JRE还是JDK
    截取url参数值
  • 原文地址:https://www.cnblogs.com/lifeisshort/p/4682977.html
Copyright © 2011-2022 走看看