Auth认证:

1.分配路由和创建视图函数

2.在视图函数中引用Django为我们提供的用户认证组建Auth

3.直接进行判断:

def login(request):
if request.method =='GET':
return render(request,'login.html')
else:
user = request.POST.get('user')
pwd= request.POST.get('pwd')
auth_obj=auth.authenticate(username=user,password=pwd)
if auth_obj:
#保存用户信息
auth.login(request,user_obj)
return redirect("/index/")
else:
return redirect("/login/") #login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
{% csrf_token %}
<input type="text" name="uer">
<input type="password" name="pwd">
<input type="submit">
</form>
</body>
</html> #index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
当前用户:{{ name }}
当前用户:{{ request.user.username }}
<a href="/logout/">注销</a>
<a href="/set_password/">修改密码</a>
</body>
</html>
session的设置
1.分配路由和创建视图函数
2.

Django默认支持Session,并且默认是将Session数据存储在数据库中,即:django_session 表中。

def login_session(request):
if request.method=='GET':
return render(request,'login.html')
else:
user=request.POST.get('uer')
pwd=request.POST.get('pwd')
print(user,pwd)
user_obj=UserInfo.objects.filter(user=user,pwd=pwd).first()
if user_obj: # 用户认证信息存储
request.session["susername"]=user
request.session["slogin"]=True '''
if request.COOKIE.get("sessionid"):
random_str=request.COOKIE.get("sessionid")
在django-seesion表中过滤session-key=random_str的记录进行update else: 1 生成一个随机字符串 23423hkjsf890234sd
2 向django-session表中插入记录
session-key session-data
23423hkjsf890234sd {"susername":"egon","slogin":True} 3 响应set_cookie : {"sessionid":23423hkjsf890234sd} '''
return redirect("/index_session/") else:
return HttpResponse('error') #取session
def index_session(request): '''
request.session
1 request.COOKIE.get("sessionid") :23423hkjsf890234sd
2 在django-session表过滤session-key=23423hkjsf890234sd的记录
3 取过滤记录的session-data反序列化成数据字典:{"susername":"egon","slogin":True} ''' slogin=request.session.get("slogin") if not slogin:
return redirect("/login_session/") susername=request.session.get("susername")
name=""
shangpin = "香蕉"
shangpin_list = ["苹果", "荔枝", "榴莲"]
return render(request, 'index.html', {'name': susername, 'shangpin': shangpin, 'shangpin_list': shangpin_list})

Django重新整理2的更多相关文章

  1. Django重新整理

    1.母版的继承 #base<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset ...

  2. Django知识点整理

    什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. web应用 访 ...

  3. django笔记整理

    Django复习: MTV模型: manager启动服务→urls找到路径→(找到views视图函数或者做路由分发)→视图函数处理相关逻辑,返回一个模板或者是字符串: ---------------- ...

  4. Django重新整理4---ModelForm-set(批量处理数据)

    1. #引用modelformset from django.forms.models import modelformset_factory #必须继承forms.ModelForm! class ...

  5. Django重新整理3

    Forms组件 1.在models.py中我们建立一个新的表关系: class UserInfo(models.Model): user=models.CharField(max_length=32) ...

  6. Django ORM整理

    字段类型 # 自增长 Auto = models.AutoField() BigAuto = models.BigAutoField() # 二进制 Binary = models.BinaryFie ...

  7. Django(一) 安装使用基础

    大纲 安装Django 1.创建Django工程 2.创建Django app 3.写一个简单的登录注册相应页面 4.获取用户请求信息并处理 5.前后端交互 6.Django 请求 生命周期  跳转到 ...

  8. python学习博客地址集合。。。

    python学习博客地址集合...   老师讲课博客目录 http://www.bootcdn.cn/bootstrap/  bootstrap cdn在线地址 http://www.cnblogs. ...

  9. 老男孩老师的博客地址 - 转自devops1992

    害怕他那天不让人看了,所以我就复制一份到我自己的博客里. http://www.bootcdn.cn/bootstrap/  bootstrap cdn在线地址 http://www.cnblogs. ...

随机推荐

  1. CodeForces 785D Anton and School - 2 (组合数学)

    题意:有一个只有’(‘和’)’的串,可以随意的删除随意多个位置的符号,现在问能构成((((((…((()))))….))))))这种对称的情况有多少种,保证中间对称,左边为’(‘右边为’)’. 析:通 ...

  2. linux通配符与基础正则

    1.特殊符号: ' '     单引号          单引号的内容     写什么就是什么,不会被当成特殊字符.       单引号实例:      [root@oldboyedu-sh01 ~] ...

  3. MyBatis的使用步骤及配置

    1.导入mybatis的jar包:mybatis-x.x.x.jar 2.使用XML配置mybatis,创建SqlSessionFactory XML配置文件的名称通常为mybatis-config. ...

  4. CI框架定义判断POST GET AJAX

    CI框架当中并没有提供,类似tp框架中IS_POST,IS_AJAX,IS_GET的方法. 所有就得我们自己造轮子了.下面就介绍一下,如何定义这些判断请求的方法.其实很简单的. 首先打开constan ...

  5. RouteSelector的初始化

    继前面的几篇OKhttp的拦截器简单分析之后,对于后续Okhttp之间的分析自己也着实琢磨了一段时间,是分析RealConnection?还是ConnectionPool,随着对Okhttp源码的深入 ...

  6. python3查询mysql数据

    python3不支持MySQLdb,代替的是import pymysql 连接数据库查表: import pymysqlconn= pymysql.connect( host='xx.xx.xx.xx ...

  7. linux脚本遇到的一点问题

    系统环境: # uname -r -.el6.x86_64 # cat /etc/redhat-release CentOS release 6.5 (Final) 对服务器状态监控的一段脚本中使用了 ...

  8. windows下eclipse远程连接hadoop集群开发mapreduce

    转载请注明出处,谢谢 2017-10-22 17:14:09  之前都是用python开发maprduce程序的,今天试了在windows下通过eclipse java开发,在开发前先搭建开发环境.在 ...

  9. 关于IBOutlet的生命周期

    在调试程序的时候,发现 IBOutlet的对象在执行 getter时,开始一直想找IBOutlet对象getter方法前被执行的代码,但是一直找不到,于是我就想是不是系统自动给IBOutlet 自动初 ...

  10. PAT天梯赛L1-020 帅到没朋友

    题目链接:点击打开链接 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. 输入格式: 输入第一行给出一个正整数N(<=100),是已知朋 ...