from django.shortcuts import render,redirect
from user.models import *
from hashlib import *
from django.http import *
from django.template import loader,RequestContext
from django.core.urlresolvers import reverse
from datetime import timedelta
from django.views.decorators.csrf import csrf_exempt
from PIL import Image, ImageDraw, ImageFont, ImageFilter
from io import StringIO,BytesIO
import random
import uuid
import os
from time import sleep
from django.conf import settings
from booktest.models import *
from user import task
# Create your views here.
def register(request):
return render(request, 'user/register.html')
def register_handler(request):
user = User()
user.uname = request.POST.get('username')
user.upwd = sha1(request.POST.get('userpwd').encode('utf-8')).hexdigest()
user.save()
return render(request, 'user/success.html')
def login(request):
print('session:%s'%request.session.get('currentUser'))
context = {}
#获取cookie
username = request.COOKIES.get('mycooki')
if username:
context['username']=username
return render(request, 'user/login.html',context)
#@csrf_exempt
def login_handler(request):
# 定义上下文
context = {}
#获取验证码
userverification = request.POST.get('userverification')
if userverification==None or request.session['codes'].upper() != userverification.upper():
context = {'userverification_error':'验证码输入错误'}
return render(request,'user/login.html',context)
#用户名密码
username = request.POST.get('username')
userpwd = sha1(request.POST.get('userpwd').encode('utf-8')).hexdigest()
#匹配
ret = User.objects.filter(uname=username,upwd=userpwd)
if len(ret)==0:
return HttpResponseRedirect('/user/login')
else:
#在服务端保持一个session键值对
request.session['currentUser'] = username
request.session.set_expiry(36000)
#request.session.set_expiry(timedelta(days=2))
#加载模板
t1 = loader.get_template('user/success.html')
#上下文
requestcontext = RequestContext(request,context)
#创建具有模板和上下文的reponse
response = HttpResponse(t1.render(requestcontext))
#记录用户名密码的变量
rememberName = request.POST.get('rememberName')
#判断
if rememberName=='1':
#写cookie
response.set_cookie('mycookie',username,max_age=3600)
return response
def verification(request):
# 240 x 60:
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建Font对象:
font = ImageFont.truetype('/usr/share/fonts/truetype/liberation/LiberationSerif-BoldItalic.ttf', 36)
# 创建Draw对象:
draw = ImageDraw.Draw(image)
# 填充每个像素:
for x in range(width):
for y in range(height):
draw.point((x, y), fill=rndColor())
#储存验证码字符串
codes = ''
# 输出文字:
for t in range(4):
code = rndChar()
codes += code
draw.text((60 * t + 10, 10),code , font=font, fill=rndColor2())
# 模糊:
image = image.filter(ImageFilter.BLUR)
#将验证码字符串存储到session中
request.session['codes'] = codes
request.session.set_expiry(0)
#内存级的字节读写
f = BytesIO()
image.save(f,'jpeg')
return HttpResponse(f.getvalue(),'image/jpeg')
def check_username(request):
username = request.GET.get('username')
# 0不存在 1存在
ret = '0'
if len(User.objects.filter(uname=username)):
ret = '1'
return HttpResponse(ret)
# 随机字母:
def rndChar():
return chr(random.randint(65, 90))
# 随机颜色1:
def rndColor():
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
# 随机颜色2:
def rndColor2():
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
def test1(request):
return render(request, 'user/test1.html')
def test3(request):
return render(request, 'user/test3.html')
def test4(request):
id = request.GET.get('id')
name = request.GET.get('name')
print('id=%s,name=%s'%(id,name))
sleep(3)
return HttpResponse('ok')
def test2(request):
#接收上传的文件
file = request.FILES.get('myfile',None)
#判断
if file:
#文件的新名字
name = doFileName(file.name)
#拼接路径
path = os.path.join(settings.MEDIA_ROOT,'images',name)
#读写文件
with open(path,'wb') as f:
for chunk in file.chunks():
f.write(chunk)
#接收其他字段,+name 存到数据库中
#文件的存放策略
else:
pass
return render(request, 'user/success.html')
#load
def test5(request):
return render(request, 'user/test5.html')
def test6(request):
return render(request, 'user/test6.html')
def test7(request):
return render(request, 'user/test7.html')
def test8(request):
#模拟耗时的操作
task.hello_world.delay()
#task.hello_world()
return render(request, 'user/test8.html')
from django.views.decorators.cache import cache_page
@cache_page(20)
def test9(request):
return HttpResponse('test099999')
#return HttpResponse('test09')
def test10(request):
return render(request, 'user/test10.html')
from django.core.cache import cache
def test11(request):
cache.set('mykey','myvalue',1000)
return render(request, 'user/test11.html')
def test12(request):
return HttpResponse(cache.get('mykey'))
没有合适的资源?快使用搜索试试~ 我知道了~
初学Python时的练习代码.zip
共2000个文件
js:1276个
py:463个
html:180个
需积分: 5 0 下载量 176 浏览量
2024-02-21
19:40:48
上传
评论
收藏 30.61MB ZIP 举报
温馨提示
初学Python时的练习代码
资源推荐
资源详情
资源评论
收起资源包目录
初学Python时的练习代码.zip (2000个子文件)
window.css 6KB
visualblocks.css 3KB
fullpage.css 2KB
media.css 1KB
props.css 892B
advimage.css 659B
popup.css 505B
advlink.css 472B
row.css 281B
template.css 252B
advhr.css 235B
cell.css 189B
attributes.css 186B
searchreplace.css 170B
table.css 157B
content.css 97B
media.htm 37KB
props.htm 37KB
link.htm 15KB
template.htm 12KB
image.htm 12KB
fullpage.htm 10KB
table.htm 9KB
ins.htm 7KB
del.htm 7KB
cell.htm 7KB
acronym.htm 6KB
row.htm 6KB
cite.htm 6KB
abbr.htm 6KB
attributes.htm 6KB
emotions.htm 6KB
searchreplace.htm 5KB
fullscreen.htm 3KB
color_picker.htm 3KB
about.htm 3KB
link.htm 2KB
rule.htm 2KB
merge_cells.htm 1KB
template.htm 1KB
pastetext.htm 1KB
anchor.htm 1KB
dialog.htm 886B
pasteword.htm 762B
blank.htm 320B
register.html 4KB
register.html 4KB
register.html 4KB
register.html 4KB
register.html 4KB
register.html 4KB
register.html 4KB
register.html 4KB
show.html 3KB
show.html 3KB
show.html 3KB
books.html 3KB
books.html 3KB
books.html 3KB
books.html 3KB
books.html 3KB
books.html 3KB
books.html 3KB
test6.html 3KB
test6.html 3KB
test6.html 3KB
hero_edit.html 2KB
hero_edit.html 2KB
test6.html 2KB
test6.html 2KB
hero_edit.html 2KB
test6.html 2KB
register.html 2KB
test7.html 2KB
test7.html 2KB
test7.html 2KB
test3.html 2KB
test3.html 2KB
test3.html 2KB
test3.html 2KB
test3.html 2KB
test3.html 2KB
test3.html 2KB
test3.html 2KB
test3.html 2KB
test3.html 2KB
bookdetail.html 2KB
bookdetail.html 2KB
bookdetail.html 2KB
bookdetail.html 2KB
bookdetail.html 2KB
bookdetail.html 2KB
bookdetail.html 2KB
bookdetail.html 2KB
bookdetail.html 2KB
bookdetail.html 2KB
bookdetail.html 2KB
hero_add.html 2KB
hero_add.html 2KB
hero_detail.html 1KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
Kwan的解忧杂货铺
- 粉丝: 1w+
- 资源: 3625
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功