下载资源后端资源详情
tedu_note.zip
大小:35KB
价格:43积分
下载量:0
评分:
5.0
上传者:HG0724
更新日期:2024-08-14

Django框架练习之云笔记项目

资源文件列表(大概)

文件名
大小
.idea/
-
.idea/.gitignore
50B
.idea/inspectionProfiles/
-
.idea/inspectionProfiles/profiles_settings.xml
174B
.idea/misc.xml
392B
.idea/modules.xml
277B
.idea/tedu_note.iml
485B
.idea/workspace.xml
2.38KB
index/
-
index/__init__.py
-
index/__pycache__/
-
index/__pycache__/__init__.cpython-36.pyc
127B
index/__pycache__/admin.cpython-36.pyc
168B
index/__pycache__/apps.cpython-36.pyc
398B
index/__pycache__/models.cpython-36.pyc
165B
index/__pycache__/views.cpython-36.pyc
304B
index/admin.py
66B
index/apps.py
148B
index/migrations/
-
index/migrations/__init__.py
-
index/migrations/__pycache__/
-
index/migrations/__pycache__/__init__.cpython-36.pyc
138B
index/models.py
60B
index/templates/
-
index/templates/index/
-
index/templates/index/index.html
815B
index/tests.py
63B
index/urls.py
-
index/views.py
139B
manage.py
687B
note/
-
note/__init__.py
-
note/__pycache__/
-
note/__pycache__/__init__.cpython-36.pyc
126B
note/__pycache__/admin.cpython-36.pyc
167B
note/__pycache__/apps.cpython-36.pyc
395B
note/__pycache__/models.cpython-36.pyc
673B
note/__pycache__/urls.cpython-36.pyc
269B
note/__pycache__/views.cpython-36.pyc
1.33KB
note/admin.py
66B
note/apps.py
146B
note/migrations/
-
note/migrations/0001_initial.py
983B
note/migrations/__init__.py
-
note/migrations/__pycache__/
-
note/migrations/__pycache__/0001_initial.cpython-36.pyc
1017B
note/migrations/__pycache__/__init__.cpython-36.pyc
137B
note/models.py
415B
note/templates/
-
note/templates/note/
-
note/templates/note/add_note.html
386B
note/templates/note/list_note.html
1.05KB
note/tests.py
63B
note/urls.py
142B
note/views.py
1.7KB
tedu_note/
-
tedu_note/__init__.py
-
tedu_note/__pycache__/
-
tedu_note/__pycache__/__init__.cpython-36.pyc
131B
tedu_note/__pycache__/settings.cpython-36.pyc
2.35KB
tedu_note/__pycache__/urls.cpython-36.pyc
1.04KB
tedu_note/__pycache__/wsgi.cpython-36.pyc
538B
tedu_note/asgi.py
411B
tedu_note/settings.py
3.71KB
tedu_note/urls.py
945B
tedu_note/wsgi.py
411B
user/
-
user/__init__.py
-
user/__pycache__/
-
user/__pycache__/__init__.cpython-36.pyc
126B
user/__pycache__/admin.cpython-36.pyc
167B
user/__pycache__/apps.cpython-36.pyc
395B
user/__pycache__/models.cpython-36.pyc
737B
user/__pycache__/urls.cpython-36.pyc
323B
user/__pycache__/views.cpython-36.pyc
2.14KB
user/admin.py
66B
user/apps.py
146B
user/migrations/
-
user/migrations/0001_initial.py
841B
user/migrations/__init__.py
-
user/migrations/__pycache__/
-
user/migrations/__pycache__/0001_initial.cpython-36.pyc
862B
user/migrations/__pycache__/__init__.cpython-36.pyc
137B
user/models.py
599B
user/templates/
-
user/templates/user/
-
user/templates/user/login.html
497B
user/templates/user/register.html
493B
user/tests.py
63B
user/urls.py
191B
user/views.py
5.14KB

资源内容介绍

Django框架练习之云笔记项目
from django.http import HttpResponse,HttpResponseRedirectfrom django.shortcuts import renderfrom .models import Userimport hashlib# Create your views here.def reg_view(request): #注册 if request.method == 'GET': # GET 返回页面 return render(request,'user/register.html') elif request.method == 'POST': username = request.POST['username'] pass1 = request.POST['password_1'] pass2 = request.POST['password_2'] # POST 处理提交数据 # 1. 密码一致 if pass1 != pass2: return HttpResponse('两次输入密码不一致!!!') # 哈希算法 - 给定明文 计算出一段定长的 不可逆的值 md5 sha-256 # 特点 # 1. 定长输出 : 不管明文输入长度多少 哈希值定长 md5 - 32位 16进制 【解释密码设置32】 # 2. 不可逆 :无法反向计算出 对应 的 明文 # 3. 雪崩效应 输入改变 输出改变 # 场景 : 1.密码处理 2.文件的完整性校验 # 如何使用 方法调用 m = hashlib.md5() m.update(pass1.encode()) # encode()变成字节串 pass1_hash = m.hexdigest() # 生成哈希值 # 2.当前用户名是否可用 检查是否注册 old_user = User.objects.filter(username=username) if old_user: return HttpResponse('用户名已注册!!!') # 插入数据 【明文处理】 try : user = User.objects.create(username=username,password=pass1_hash) except Exception as e: # 由于唯一索引 报错 重复插入 【唯一索引注意并发写入问题】 print('--create user error %s' % (e)) return HttpResponse('用户名已注册') return HttpResponse('注册成功!')# 优化建议 : 密码问题 创建语句问题# 请求量大 User.objects.create 会报错 在username该字段 因为是唯一字段 可能由于并发注册问题 发生重复写入问题 # 免登录一天 session 用户名 主键 存入 session request.session['username'] = username request.session['uid'] = user.id # TODO 修改session存储时间为1天 settings.py # return HttpResponse('注册成功') return HttpResponseRedirect('/index')def login_view(request): if request.method == 'GET': # 获取登录页面 # 检查登录状态 登陆过 显示已登录 # 先检查session if request.session.get('username') and request.session.get('uid'): # return HttpResponse('已登录') return HttpResponseRedirect('/index') # 检查cookie c_username = request.COOKIES.get('username') c_uid = request.COOKIES.get('uid') if c_username and c_uid: # 回写到session中 request.session['username'] = c_username request.session['uid'] = c_uid # return HttpResponse('已登录') return HttpResponseRedirect('/index') # GET 返回页面 return render(request,'user/login.html') elif request.method == 'POST': # 获得数据 username = request.POST['username'] password = request.POST['password'] # 查询 是否有此人 # username 是唯一索引 try: user = User.objects.get(username=username) # 找不到 肯定没有 except Exception as e: print('--login user error %s' % (e)) return HttpResponse('用户名或者密码错误!!!') # 比对密码 因为哈希不可逆 所以从新生成哈希 用哈希数值进行比对 m = hashlib.md5() m.update(password.encode()) if m.hexdigest() != user.password: return HttpResponse('用户名或密码错误') # 记录会话状态 # 免登录一天 session 用户名 主键 存入 session request.session['username'] = username request.session['uid'] = user.id # 判断有没有✔ 通过检查浏览器响应分析 checkbox的状态 remember=on # #点选了->Cookies存储username,uid时间3天 # resp = HttpResponse('--------success---------') resp = HttpResponseRedirect('/index') # print('request.POST:',request.POST) # request.POST: <QueryDict: {'username': ['zhenhao2'], 'password': ['123'], 'remember': ['on']}> if 'remember' in request.POST: resp.set_cookie('username', username, 3600 * 24 * 3) resp.set_cookie('uid', user.id, 3600 * 24 * 3) return resp# 退出def logout_view(request): # 删除session if 'username' in request.session: del request.session['username'] if 'uid' in request.session: del request.session['uid'] # 检查cookie 删除cookie需要一个响应对象 resp = HttpResponseRedirect('/index') if 'username' in request.COOKIES: resp.delete_cookie('username') if 'uid' in request.session: resp.delete_cookie('uid') return resp

用户评论 (0)

发表评论

captcha