django-項目開發筆記. 開發版本號1.11.8[pip3 install django==1.11.8]

  1. 登錄驗證問題
    1.1 使用django自帶的登錄驗證功能
    我這裏對比的是是django庫中的,auth_user的表;
    post發送過來的賬號密碼對比;
#導入驗證的模塊
from django.contrib.auth import authenticate,logout,login
def login(request):
	username=request.POST['username']
	password=request.POST['pwd']
	user=authenticate(username=username,password=password)
	if user is not None:
		print("登錄成功!")
		# 記錄登錄成功的狀態
		login(request,user)
	else:
		print("登錄失敗!")
1.2 在views.py中,獲取已登錄狀態的username
def *_views(request):
	show_name=request.user.username
1.3 使用django自帶的重定向,未登錄則跳轉至登錄頁
#導入模塊,裝飾器使用
from django.contrib.auth.decorators import login_required
@login_required()
def index_views(request):
	return render(request,'index.html',locals())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章