驗證碼功能:css
def get_img(request): import random def get_random_img(): return (random.randint(0,255),random.randint(0,255),random.randint(0,255)) from PIL import Image,ImageDraw,ImageFont img = Image.new("RGB",(270,40),color=get_random_img()) #方式二 # with open('valid_code.png','wb') as f: # img.save(f) # # with open('valid_code.png','rb') as f: # data = f.read() from io import BytesIO draw = ImageDraw.Draw(img) chandan_ttf = ImageFont.truetype("static/font/chandan.ttf",size=32) for i in range(0,5): num = str(random.randint(0,9)) #生成隨機數字 upper_alpha = chr(random.randint(65,90)) #生成隨機大寫字母 low_alpha = chr(random.randint(97,122)) #生成隨機小寫字母 now_char = random.choice([num,upper_alpha,low_alpha]) #使用隨機列表從中選擇 draw.text((i*50,5),now_char,get_random_img(),font=chandan_ttf) #draw.text(xy座標,寫的內容text,背景顏色,字體ttf) f = BytesIO() img.save(f,"png") data = f.getvalue() return HttpResponse(data)
儲存驗證碼的值:經過session來儲存,這樣每一個瀏覽器就會獨享一個驗證碼html
request.session['valid_code_str'] = valid_code_str #valid_code_str 爲系統隨機生成的驗證碼
這時再在index視圖函數中取出該sessionweb
valid_code_str = request.session.get('valid_code_str') if valid_code_str.upper() == valid_code.upper(): #不區分大小寫 pass else: respon_dic['msg'] = "valid error" return JsonResponse(respon_dic)
JsonResponse:能夠幫你序列化
#若是這樣返回,ajax還須要進行json解析 #views.py return HttpResponse(json.dumps({"msg":"ok!"})) #index.html var data=json.parse(data)console.log(data.msg); #若是這樣返回,兩邊都不須要進行json的序列化與反序列化,ajax接受的直接是一個對象 #views.py from django.http import JsonResponse return JsonResponse({"msg":"ok!"}) #index.html console.log(data.msg);
當用戶選擇文件時,選取當前用戶的文件對象ajax
$("#avater")[0].files[0]
File(244782) {name: "sedarkwallpaper.srx", lastModified: 1540678438189, lastModifiedDate: Sun Oct 28 2018 06:13:58 GMT+0800 (中國標準時間), webkitRelativePath: "", size: 244782, …}數據庫
Media配置:下降耦合性django
Django有兩種靜態文件:json
1./static/ :css,js,img瀏覽器
2./media/ 用戶上傳文件session
media配置之MEDI_ROOTdom
在settings.py 文件裏面設置
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
在Django中將會實現:
用戶上傳的文件將會下載至MEDIA_ROOT的media文件下,若是沒有將會自動建立
media 配置之MEDIA_URL
目的是瀏覽器能夠直接訪問到media數據
MEDIA_URL = '/media/'
在url.py中再設置:
from django.views.static import serve re_path(r'media/(?P<path>.*)$',serve,{"document_root":settings.MEDIA_ROOT}),
使用Django裏面的admin組件建立表:
在admin.py下:
首先是註冊,將建立的表往admin裏面註冊,而後打開http://127.0.0.1:8000/admin,能夠查看
from my_blog import models admin.site.register(models.UserInfo) admin.site.register(models.Article) admin.site.register(models.Article2Tag) admin.site.register(models.ArticleUpDown) admin.site.register(models.Category) admin.site.register(models.Blog) admin.site.register(models.Comment) admin.site.register(models.Tag)
vertical-align: -1px; 相對位移
我的站點的日期查詢:
1.date_format;
============date,time,datetime================
create table t_mul(d date,t time,dt datetime); //date:年月日;time:時分秒;datetime:年月日時分秒
insert into t_mul values(now(),now(),now());
select date_format(dt,"%Y/%m/%d") from t_mul
2.extra;
extra(self, select=None, where=None, params=None, tables=None,
order_by=None, select_params=None):
參數select
date_list=models.Article.objects.filter(user=user).extra(select={"y_m_date":"date_format(create_time,'%%Y/%%m')"}).values("y_m_date").annotate(c=Count("nid")).values_list("y_m_date","c")
方式二:
# 方式2: # from django.db.models.functions import TruncMonth # # ret=models.Article.objects.filter(user=user).annotate(month=TruncMonth("create_time")).values("month").annotate(c=Count("nid")).values_list("month","c") # print("ret----->",ret)
<scrip>alrent('123')</script>
使用utf-8生成數據庫
CREATE DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
刪除數據庫
drop database <數據庫名>