• 【Django笔记】 登录功能


    1.  post请求 

    sign/templates/index.html

    其中 : 

    1. "en">
    2. "UTF-8">
    3. Title
    4. 发布会管理

    5. "post">
    6. <input name="username" type="text" placeholder="username">
    7. <input name="password" type="password" placeholder="password">

    2.出现跨站请求伪造 

     Django 正对CSRF 的保护错误是生成的每个表单中放置一个自动生成令牌。通过这个令牌胖墩POST请求是否来自同一个网站。form 添加 { %   csrf_token %}

    1. "en">
    2. "UTF-8">
    3. Title
    4. 发布会管理

    5. "post">
    6. <input name="username" type="text" placeholder="username">
    7. <input name="password" type="password" placeholder="password">
    8. {% csrf_token %}

     

     

     3.Cookie  和Session 

    Cookie  机制 : 

    Cookie 分发通过拓展Http 协议来实现,服务器通过在http 的响应头中加上一行特殊的指示来提示浏览器按照只是生成相应的Cookie. 

    Session机制:  Session 机制 是一种服务器的机制,服务器使用一种雷士与散列表的结构,保存信息。

    Cookie 使用

    event_manage.html

    1. "en">
    2. "UTF-8">
    3. Title
    4. Login Success !

    5. "float:right;">
  • 4.Cookie

    sign/views.py

    设置cookie 的值

    response= HttpResponseRedirect('/event_manage/')  # 重定向
                response.set_cookie('user',username,3600)  # 添加浏览器
                return response

    # 发布会管理 --取出cookie 的值
    def event_manage(request):
        username=request.COOKIES.get("user","")# 读取浏览器cookie
        return render(request, "event_manage.html",{"user":username}) 

    1. from django.shortcuts import render
    2. # Create your views here.
    3. from django.http import HttpResponse
    4. from django.http import HttpResponse, HttpResponseRedirect
    5. # 定义inex 函数,通过HttpResponse 类向客户端返回字符创
    6. def index(request):
    7. # return HttpResponse("Hello Django!") # 使用Django 的render函数
    8. return render(request, "index.html")
    9. def login_action(request):
    10. if request.method == "POST":
    11. username = request.POST.get('username', "") # 字段对应表单的input属性
    12. password = request.POST.get('password', '')
    13. if username == 'admin' and password == "admin123":
    14. # return HttpResponse('login success')
    15. response= HttpResponseRedirect('/event_manage/') # 重定向
    16. response.set_cookie('user',username,3600) # 添加浏览器
    17. return response
    18. else:
    19. return render(request, "index.html", {'error': 'username or password error!'})
    20. # 发布会管理
    21. def event_manage(request):
    22. username=request.COOKIES.get("user","")# 读取浏览器cookie
    23. return render(request, "event_manage.html",{"user":username})

     

    5.Session 

    1. from django.shortcuts import render
    2. # Create your views here.
    3. from django.http import HttpResponse
    4. from django.http import HttpResponse, HttpResponseRedirect
    5. # 定义inex 函数,通过HttpResponse 类向客户端返回字符创
    6. def index(request):
    7. # return HttpResponse("Hello Django!") # 使用Django 的render函数
    8. return render(request, "index.html")
    9. def login_action(request):
    10. if request.method == "POST":
    11. username = request.POST.get('username', "") # 字段对应表单的input属性
    12. password = request.POST.get('password', '')
    13. if username == 'admin' and password == "admin123":
    14. # return HttpResponse('login success')
    15. response= HttpResponseRedirect('/event_manage/') # 重定向
    16. #response.set_cookie('user',username,3600) # 添加浏览器
    17. request.session['user']=username # 将session 信息记录到浏览器
    18. return response
    19. else:
    20. return render(request, "index.html", {'error': 'username or password error!'})
    21. # 发布会管理
    22. def event_manage(request):
    23. #username=request.COOKIES.get("user","")# 读取浏览器cookie
    24. username=request.session.get("user","") # 读取浏览器session
    25. return render(request, "event_manage.html",{"user":username})

    1. return super().execute(sql, params)
    2. File "D:\software\python3\anconda3\Lib\site-packages\django\db\backends\utils.py", line 67, in execute
    3. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
    4. File "D:\software\python3\anconda3\Lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
    5. return executor(sql, params, many, context)
    6. File "D:\software\python3\anconda3\Lib\site-packages\django\db\backends\utils.py", line 80, in _execute
    7. with self.db.wrap_database_errors:
    8. File "D:\software\python3\anconda3\Lib\site-packages\django\db\utils.py", line 89, in __exit__
    9. raise dj_exc_value.with_traceback(traceback) from exc_value
    10. File "D:\software\python3\anconda3\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    11. return self.cursor.execute(sql, params)
    12. File "D:\software\python3\anconda3\Lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
    13. return Database.Cursor.execute(self, query, params)
    14. django.db.utils.OperationalError: no such table: django_session
    15. [01/Oct/2023 16:18:46] "POST /login_action/ HTTP/1.1" 500 140361

     

     通过 migrate 命令进行数据迁移。

     

     

  • 相关阅读:
    字节跳动基础架构编排调度团队论文入选云计算领域顶会 SoCC 2023
    Spark【RDD编程(三)键值对RDD】
    JS利用循环解决的一些问题
    Django Web架构:全面掌握Django模型字段(上)
    leetcode每天5题-Day41(二叉树7-二叉搜索树)
    Java“牵手”义乌购商品详情数据,义乌购商品详情接口,义乌购API接口申请指南
    博睿数据出席阿里云可观测技术峰会,数字体验管理驱动可持续发展
    springboot医院HIS信息管理系统源码:技术架构:Angular+Nginx+Java+Spring,SpringBoot
    异步编程 - 03 线程池ThreadPoolExecutor原理剖析&源码详解
    javaScript:DOM中常用尺寸
  • 原文地址:https://blog.csdn.net/oDianZi1234567/article/details/133459066