当前位置:   article > 正文

基于 django 自带的用户认证进行用户认证

引用django用户认证

基于 django 自带的用户认证进行用户认证

django admin 默认已经存在了一个用户认证,这个时候可以偷个小懒,直接用 django 自带的,就不需要自己写用户认证了

1、目录结构:

 

 

2、代码 

  • 在 settings.py 中添加一行
    1. # 如果不添加该行,则在未登录状态打开页面的时候验证是否登录的装饰器跳转到 /accounts/login/ 下面
    2. # 第一种解决方法就是修改 settings.py 中的 LOGIN_URL
    3. # 第二种解决方法是在 url 中匹配该 url
    4. LOGIN_URL = "/login/"

     

  • CRM/app01/models.py
    1. from django.db import models
    2. from django.contrib.auth.models import User
    3. class UserProfile(models.Model):
    4. """账户信息表"""
    5. user = models.OneToOneField(User) # 跟 django 的 User 表做一个一对一
    6. name = models.CharField(max_length=32)
    7. roles = models.ManyToManyField("Role", blank=True, null=True)

     

  • CRM/CRM/urls.py
    1. from django.conf.urls import url, include
    2. from django.contrib import admin
    3. from CRM import views
    4. urlpatterns = [
    5. url(r'^crm/', include("app01.urls")),
    6. url(r'^login/', views.acc_login),
    7. url(r'^logout/', views.acc_logout),
    8. ]
    View Code

     

  • CRM/CRM/views.py
    1. #!/usr/bin/env python
    2. # -*- coding: utf-8 -*-
    3. # Author:zhangcong
    4. # Email:zc_92@sina.com
    5. from django.contrib.auth import authenticate, login, logout
    6. from django.shortcuts import render
    7. from django.shortcuts import redirect
    8. def acc_login(request):
    9. """登录"""
    10. print(request.user)
    11. if request.method == "POST":
    12. username = request.POST.get("username")
    13. password = request.POST.get("password")
    14. user = authenticate(username=username, password=password) # 调用 django 的认证模块进行认证
    15. if user: # 判断验证是否通过
    16. login(request, user)
    17. next_url = request.GET.get('next', None)
    18. if not next_url:
    19. next_url = '/crm/'
    20. return redirect(next_url)
    21. return render(request, 'login.html')
    22. def acc_logout(request):
    23. """登出"""
    24. logout(request)
    25. return redirect('/login/')
    View Code

     

  • CRM/templates/login.html
    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="utf-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1">
    7. <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    8. <meta name="description" content="">
    9. <meta name="author" content="">
    10. <link rel="icon" href="http://v3.bootcss.com/favicon.ico">
    11. <title>Signin Template for Bootstrap</title>
    12. <!-- Bootstrap core CSS -->
    13. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    14. <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    15. <link href="http://v3.bootcss.com/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
    16. <!-- Custom styles for this template -->
    17. <link href="http://v3.bootcss.com/examples/signin/signin.css" rel="stylesheet">
    18. <style>
    19. .line{
    20. margin-top: 5px;
    21. }
    22. </style>
    23. </head>
    24. <body>
    25. <div class="container">
    26. <form class="form-signin" method="POST">
    27. {% csrf_token %}
    28. <h2 class="form-signin-heading">Please sign in</h2>
    29. <div class="line">
    30. <label for="username" class="sr-only">UserName</label>
    31. <input type="text" id="username" class="form-control" name="username" placeholder="UserName" required autofocus>
    32. </div>
    33. <div class="line">
    34. <label for="inputPassword" class="sr-only">Password</label>
    35. <input type="password" id="inputPassword" class="form-control" name="password" placeholder="Password" required>
    36. </div>
    37. <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
    38. </form>
    39. </div> <!-- /container -->
    40. </body>
    41. </html>
    View Code

     

  • CRM/app01/urls.py  
    1. from django.conf.urls import url, include
    2. from app01 import views
    3. urlpatterns = [
    4. url(r'^$', views.dashboard),
    5. ]
  • CRM/app01/views
    1. from django.shortcuts import HttpResponse
    2. from django.contrib.auth.decorators import login_required
    3. @login_required # 验证是否登录
    4. def dashboard(request):
    5. print(request.user)
    6. return HttpResponse('login ok!')

     

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/71675?site
推荐阅读
相关标签
  

闽ICP备14008679号