django搭建BBS 登入&驗證碼的生成 文件結構 app 介面 migrations _\_inint\_\_.py admin.py apps.py bbsform.py models.py tests.py views.py avatar BBS \_\_inint\_\_.py setti ...
django搭建BBS-登入&驗證碼的生成
基於註冊完成後
文件結構
- app 介面
- migrations
- __inint__.py
- admin.py
管理員頁面註冊表單用
- apps.py
- bbsform.py
form組件相關設置
- models.py
模型存放
- tests.py
- views.py
業務邏輯
- avatar
圖片文件存儲
- BBS
項目名稱以及路由存放
- __inint__.py
- settings.py
- urls.py
- wsgi.py
- static
- bootstrap-3.3.7-dist
bootstrap文件網上下載的
- jquery-3.4.1.min.js
jq文件
- bootstrap-3.3.7-dist
- templates
頁面文件存放
一.創建圖片驗證
1.路由
urls.py
from django.conf.urls import url
from django.contrib import admin
#主路由導入視圖內函數
from app import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^register/', views.register),
url(r'^login/', views.login),
url(r'^get_code/', views.get_code),
]
2.邏輯業務
views.py
from django.shortcuts import render,HttpResponse,redirect
from django.http import JsonResponse
#Image導入
#ImageDraw在圖片上寫字
#ImageFont 寫字的格式
from PIL import Image,ImageDraw,ImageFont
import random
# 相當於把文件以byte格式存到記憶體中
from io import BytesIO
from django.contrib import auth
from app.bbsforms import Register
from app import models
from django.db.models import Count
from django.db.models.functions import TruncMonth
from django.db.models import F
# Create your views here.
def register(request):
if request.method=='GET':
form=Register()
return render(request,'register.html',{'form':form})
elif request.is_ajax():
response={'code':100,'msg':None}
form = Register(request.POST)
if form.is_valid():
#校驗通過的數據
clean_data=form.cleaned_data
#把re_pwd剔除
clean_data.pop('re_pwd')
#取出頭像
avatar=request.FILES.get('avatar')
if avatar:
#因為用的是FileField,只需要把文件對象賦值給avatar欄位,自動做保存
clean_data['avatar']=avatar
user=models.UserInfo.objects.create_user(**clean_data)
if user:
response['msg'] = '創建成功'
else:
response['code'] = 103
# 把校驗不通過的數據返回
response['msg'] = '創建失敗'
else:
response['code']=101
#把校驗不通過的數據返回
response['msg']=form.errors
print(type(form.errors))
return JsonResponse(response,safe=False)
def login(request):
if request.method=='GET':
return render(request,'login.html')
def get_code(request):
if request.method == 'GET':
img = Image.new('RGB', (350, 40), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
# 寫文字
# 生成一個字體對象
font = ImageFont.truetype('/static/Gabriola.ttf', 34)
# 調用方法,返回一個畫板對象
draw = ImageDraw.Draw(img)
new_text =''
# 生成隨機8位數字
for x_index in range(1, 8):
num = chr(random.randint(48, 57))
word = chr(random.randint(65, 90))
word_1 = chr(random.randint(97, 122))
text =random.choice((num, word, word_1))
draw.text((x_index * 32, 0),text, font=font)
new_text +=text
# 加點線
width = 320
height = 35
for i in range(5):
x1 = random.randint(0, width)
x2 = random.randint(0, width)
y1 = random.randint(0, height)
y2 = random.randint(0, height)
# 在圖片上畫線
draw.line((x1, y1, x2, y2), fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
for i in range(33):
# 畫點
draw.point([random.randint(0, width), random.randint(0, height)], fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
x = random.randint(0, width)
y = random.randint(0, height)
# 畫弧形
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
print(new_text)
#存在session中
request.session['code']=new_text
#存記憶體
f = BytesIO()
img.save(f, 'png')
return HttpResponse(f.getvalue())
3.網頁
login.html
<!DOCTYPE html>
<html lang="en">
<head>
{% include 'bootstrap.html' %}<--載入bootstrap-->
<meta charset="UTF-8">
<title>登入</title>
</head>
<body>
<div class="container-fluid center-block">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1>登陸</h1>
<form action="">
<div class="form-group">
<label for="id_name">用戶名</label>
<input type="text" name="name" id="id_name" class="form-control">
</div>
<div class="form-group">
<label for="pwd">密碼</label>
<input type="password" name="pwd" id="pwd" class="form-control">
</div>
<div class="form-group">
<label for="id_code">驗證碼</label>
<div class="row">
<div class="col-md-6">
<input type="text" name="code" id="id_code" class="form-control">
</div>
<div class="col-md-6" id="img">
<img src="/get_code/" height="40" width="350" class="img-code">
</div>
</div>
</div>
<input type="submit" value="提交" class="btn-success">
</form>
</div>
</div>
</div>
</body>
{% include 'jq.html' %} <--載入jq-->
<script> <--利用img標標簽屬性src發送改變後會自己去找-->
$('.img-code').click(function () {
var img_code_src = $(this).attr('src');
img_code_src += '1';
console.log(img_code_src);
$(this).attr('src',img_code_src)
})
</script>
</html>
二.賬號信息進行驗證
login.html
<!DOCTYPE html>
<html lang="en">
<head>
{% include 'bootstrap.html' %}
<meta charset="UTF-8">
<title>登入</title>
</head>
<body>
<div class="container-fluid center-block">
<div class="row">
<div class="col-md-6 col-md-offset-3">
{% csrf_token %}
<h1>登陸</h1>
<form action="">
<div class="form-group">
<label for="id_name">用戶名</label>
<input type="text" name="name" id="id_name" class="form-control">
</div>
<div class="form-group">
<label for="pwd">密碼</label>
<input type="password" name="pwd" id="pwd" class="form-control">
</div>
<div class="form-group">
<label for="id_code">驗證碼</label>
<div class="row">
<div class="col-md-6">
<input type="text" name="code" id="id_code" class="form-control">
</div>
<div class="col-md-6" id="img">
<img src="/get_code/" height="40" width="350" class="img-code">
</div>
</div>
</div>
<input type="button" value="提交" class="btn-success" id="up_data">
<span style="color: red" id="msg"></span>
</form>
</div>
</div>
</div>
</body>
{% include 'jq.html' %}
<script>
$('.img-code').click(function () {
var img_code_src = $(this).attr('src');
img_code_src += '1';
console.log(img_code_src);
$(this).attr('src',img_code_src)
})
</script>
<script>
$('#up_data').click(function () {
$.ajax({
type:'post',
url:'/login/',
data:{'name':$('#id_name').val(),
'pwd':$('#pwd').val(),
'code':$('#id_code').val(),
'csrfmiddlewaretoken':'{{csrf_token}}'
},
success:function (msg) {
console.log(msg);
$('#msg').text(msg);
if (msg =='登入成功'){
console.log('sb');
window.location.replace('http://www.baidu.com');<--暫時先放百度-->
}
}
})
})
</script>
</html>
views.py
from django.shortcuts import render,HttpResponse,redirect
from django.http import JsonResponse
#Image導入
#ImageDraw在圖片上寫字
#ImageFont 寫字的格式
from PIL import Image,ImageDraw,ImageFont
import random
# 相當於把文件以byte格式存到記憶體中
from io import BytesIO
from django.contrib import auth
from app.bbsforms import Register
from app import models
from django.db.models import Count
from django.db.models.functions import TruncMonth
from django.db.models import F
# Create your views here.
def register(request):
if request.method=='GET':
form=Register()
return render(request,'register.html',{'form':form})
elif request.is_ajax():
response={'code':100,'msg':None}
form = Register(request.POST)
if form.is_valid():
#校驗通過的數據
clean_data=form.cleaned_data
#把re_pwd剔除
clean_data.pop('re_pwd')
#取出頭像
avatar=request.FILES.get('avatar')
if avatar:
#因為用的是FileField,只需要把文件對象賦值給avatar欄位,自動做保存
clean_data['avatar']=avatar
user=models.UserInfo.objects.create_user(**clean_data)
if user:
response['msg'] = '創建成功'
else:
response['code'] = 103
# 把校驗不通過的數據返回
response['msg'] = '創建失敗'
else:
response['code']=101
#把校驗不通過的數據返回
response['msg']=form.errors
print(type(form.errors))
return JsonResponse(response,safe=False)
def login(request):
if request.method=='GET':
return render(request,'login.html')
else:
print(request.POST)
user_name=request.POST.get('name')
pwd=request.POST.get('pwd')
code=request.POST.get('code')
user=auth.authenticate(username=user_name,password=pwd)
print(user)
if request.session.get('code').upper() !=code.upper(): #忽略大小寫
return HttpResponse('驗證碼錯誤')
elif not user:
return HttpResponse('賬號密碼錯誤')
else:
return HttpResponse('登入成功')
def get_code(request):
if request.method == 'GET':
img = Image.new('RGB', (350, 40), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
# 寫文字
# 生成一個字體對象
font = ImageFont.truetype('/static/Gabriola.ttf', 34)
# 調用方法,返回一個畫板對象
draw = ImageDraw.Draw(img)
new_text =''
# 生成隨機8位數字
for x_index in range(1, 8):
num = chr(random.randint(48, 57))
word = chr(random.randint(65, 90))
word_1 = chr(random.randint(97, 122))
text =random.choice((num, word, word_1))
draw.text((x_index * 32, 0),text, font=font)
new_text +=text
# 加點線
width = 320
height = 35
for i in range(5):
x1 = random.randint(0, width)
x2 = random.randint(0, width)
y1 = random.randint(0, height)
y2 = random.randint(0, height)
# 在圖片上畫線
draw.line((x1, y1, x2, y2), fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
for i in range(33):
# 畫點
draw.point([random.randint(0, width), random.randint(0, height)], fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
x = random.randint(0, width)
y = random.randint(0, height)
# 畫弧形
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
print(new_text)
#存在session中
request.session['code']=new_text
#存記憶體
f = BytesIO()
img.save(f, 'png')
return HttpResponse(f.getvalue())