• 每日作业day69


    1.整理今日内容到博客
    2.利用session实现登陆验证

    urls.py

    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        # 登录功能
        url(r'^login/', views.login),
        url(r'^home/', views.home),
        url(r'^index/', views.index),
        url(r'^teacher/', views.teacher),
        url(r'^logout/', views.logout),
    ]
    urls.py

    views.py

    from django.shortcuts import render,HttpResponse,redirect
    
    # Create your views here.
    
    # 校验用户是否登录的装饰器
    """
    用户如果在没有登陆的情况下想访问一个需要登陆的页面
    那么先跳转到登陆页面 当用户输入正确的用户名和密码之后
    应该跳转到用户之前想要访问的页面去 而不是直接写死
    """
    def login_auth(func):
        def inner(request,*args,**kwargs):
            # print(request.path_info)
            # print(request.get_full_path())
            target_url = request.get_full_path()   # 能够获取到用户上一次想要访问的url
            if request.session.get("username"):
                return func(request,*args,**kwargs)
            else:
                return redirect("/login/?next=%s" %target_url)
        return inner
    
    def login(request):
        if request.method == "POST":
            username = request.POST.get("username")
            password = request.POST.get("password")
            if username == "jason" and password == "111":
                # 拿到用户上一次想要访问的页面
                target_url = request.GET.get('next')  # 这个结果可能是None
                if target_url:
                    pass
                else:
                    # 保存用户登陆状态
                    target_url='/home/'
                request.session["username"] = "jason"
    
                # 跳转到一个需要用户登录之后才能看的页面
                return redirect(target_url)
        return render(request,"login.html")
    
    # 注销
    @login_auth
    def logout(request):
        # 注销后跳转到首页
        request.session.flush()
        return redirect("/login/")
    
    @login_auth
    def home(request):
        return HttpResponse("我是home页面,只有登录之后才能观看")
    
    @login_auth
    def index(request):
        return HttpResponse("我是index页面,只有登录之后才能观看")
    
    @login_auth
    def teacher(request):
        return HttpResponse("我是teacher页面,只有登录之后才能观看")
    views.py

    login.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>登录界面</title>
        <!-- Bootstrap3 核心 CSS 文件 -->
        <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
        <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
        <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
        <!-- Bootstrap3 核心 JavaScript 文件 -->
        <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <!-- font-awesome.min.css图标库4.7版本 -->
        <link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    
    </head>
    <body>
        <div class="container-fluid">
          <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <form action="" method="post">
                        <p>username:<input type="text" name="username" class="form-control"></p>
                        <p>password:<input type="password" name="password" class="form-control"></p>
                        <input type="submit" class="btn btn-info btn-block">
                    </form>
                </div>
          </div>
        </div>
    </body>
    </html>
    login.html

    3.复习django阶段所学所有知识点,好好整理回顾(后面没时间了)
    4.预习内容:
    https://www.cnblogs.com/Dominic-Ji/p/10881214.html
      django中间件
      auth模块

  • 相关阅读:
    Android adb shell command
    Using SQLite in Android Application
    Decompiler APK
    Java Invoke OCX
    Debug Android Application
    Working with SharePoint’s Discussion Lists Programmatically – Part 2
    SharePoint 2010 Performance Point Service Configuration and Utilization
    SharePOint 翻译界面的UI
    Sharepoint 2010 Character problem in Category Titles in Blog Site for different languages
    Working with SharePoint’s Discussion Lists Programmatically – Part 1
  • 原文地址:https://www.cnblogs.com/baicai37/p/13052784.html
Copyright © 2020-2023  润新知