from django.conrtrib import auth
它提供了用戶認證,即驗證用戶名以及密碼是否正確,通常須要username和password兩個關鍵字參數。若是認證信息有效,會返回一個 User 對象。authenticate()會在User 對象上設置一個屬性標識那種認證後端認證了該用戶,且該信息在後面的登陸過程當中是須要的。當咱們試圖登陸一個從數據庫中直接取出來不通過authenticate()的User對象會報錯的!!css
user_obj = auth.authenticate(username="someone",password="somepassword") # 去auth_user表中查詢記錄,查詢成功則返回用戶對象,查詢失敗返回None
該函數接受一個HttpRequest對象,以及一個認證了的User對象。html
此函數使用django的session框架給某個已認證的用戶附加上sessionid等信息。算法
from django.contrib import auth def my_view(request): username = request.POST['username'] password = request.POST['password'] user_obj = auth.authenticate(username=username, password=password) if not user_obj: # 成立表示去auth_user表認證成功 auth.login(request, user_obj) # 則保存用戶狀態 # Redirect to a success page. else: # Return an 'invalid login' error message.
該函數接受一個HttpRequest對象,無返回值。當調用該函數時,當前請求的session信息會所有清除。該用戶即便沒有登陸,使用該函數也不會報錯。數據庫
from django.contrib import auth def logout_view(request): auth.logout(request) # Redirect to a logout success page
from django.contrib.auth.models import User
若是是真正的 User 對象,is_authenticated()的返回值恆爲 True 。用於檢查用戶是否已經經過了認證。經過認證並不意味着用戶擁有任何權限,甚至也不檢查該用戶是否處於激活狀態,這只是代表用戶成功的經過了認證。這個方法很重要, 在後臺用request.user.is_authenticated()判斷用戶是否已經登陸,若是true則能夠向前臺展現request.user.username。npm
現有需求:django
a、用戶登錄後才能訪問某些頁面;bootstrap
b、若是用戶沒有登陸就訪問該頁面的話直接跳到登陸頁面;後端
c、用戶在跳轉的登錄界面中完成登錄後,自動訪問跳轉到以前訪問的地址;session
方法一: app
def my_view(request): if not request.user.is_authenticated(): return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
方法二:django已經爲咱們設計好了一個用於此種狀況的裝飾器:login_requierd()
from django.contrib.auth.decorators import login_required @login_required def my_view(request): ...
若用戶沒有登陸,則會跳轉到django默認的登陸URL '/accounts/login/ ' (這個值能夠在settings文件中經過LOGIN_URL進行修改,如:LOGIN_URL='/login/')。並傳遞當前訪問url的絕對路徑 (登錄成功後,會重定向到該路徑)。
from django.contrib.auth.models import User # 向auth_user表中添加一條用戶信息記錄 user = User.objects.create_user(username='',password='',email='') # 跟之前的objects.create()的區別是create_user添加的記錄密碼字段是加密的
# 用戶要修改密碼的時候,首先讓他輸入原來的密碼,若給定的字符串經過了密碼檢查,返回True check_password(passwd)
# 使用set_password() 來修改密碼 user = User.objects.get(username=request.user.username) user.set_password(raw_password="newpassword") user.save()
def sign_up(request): state = None if request.method == 'POST': password = request.POST.get('password', '') repeat_password = request.POST.get('repeat_password', '') email = request.POST.get('email', '') username = request.POST.get('username', '') if User.objects.filter(username=username): state = 'user_exist' else: new_user=User.objects.create_user(username=username, password=password,email=email) new_user.save() return redirect('/book/') content = { 'state': state, 'user': None,} return render(request, 'sign_up.html', content)
@login_required def set_password(request): user = request.user state = None if request.method == 'POST': old_password = request.POST.get('old_password', '') new_password = request.POST.get('new_password', '') repeat_password = request.POST.get('repeat_password', '') if user.check_password(old_password): if not new_password: state = 'empty' elif new_password != repeat_password: state = 'repeat_error' else: user.set_password(new_password) user.save() return redirect("/log_in/") else: state = 'password_error' content = { 'user': user, 'state': state, } return render(request, 'set_password.html', content)
一、用戶發送get請求,其信息是存儲在 request.GET 中,而request.GET 是QueryDict類型的,該類型取值的方式與普通的字典相似,可是該字典是可讀,不可寫的,若是想要修改該字典中的內容須要對其進行深拷貝,才能修改
{"user":user,"page":2} # QueryDict類型,與普通字典長的同樣,不可修改
二、如何修改?(深拷貝後獲得的能夠修改)
# 若請求中 ?a=1&b=2 request.GET["xxx"]=123 print(type(request.GET)) # 報錯 不可直接修改 import copy params = copy.deepcopy(request.GET) #深拷貝後能夠修改 params["xxx"]=123 print("params",params) print(params.urlencode()) # "a=1&b=2&xxx=123"
分析:經過 urlencode()方法能夠反解爲請求時的 urlencode數據類型,所以該方法能夠用於保存搜索條件,經過將新的參數添加到 parmars 中,而後轉成 urlencode類型就能夠做爲,請求時的路徑,此時路徑就保存了搜索路徑的信息。
三、請求的分類
post 請求:通常是用來提交數據的(添加和修改),參數會有 contentType 數據格式
get 請求:通常是用來查詢的,參數是固定的 urlencode 類型
delete 請求:通常是用來刪除的,
數據庫中有大量的數據時,在分多頁展現的時候,須要用到分頁展現數據,
首先想數據庫中批量插入數據:
#批量插入數據: # for i in range(100): # Book.objects.create(title="book_%s"%i,price=i*i) #該方法較慢 book_list=[] for i in range(100): book=Book(title="book_%s"%i,price=i*i) book_list.append(book) Book.objects.bulk_create(book_list)
該方法實現了 保存搜索條件 的功能
一、分頁的使用(views.py)
from app01.models import Book from django.core.paginator import Paginator,EmptyPage def index(request):from app01.page import Pagination # 引入分頁的類 current_page_num = request.GET.get("page",1) # 從發送的請求中,?參數,獲取值 book_list = Book.objects.all() pagination=Pagination(current_page_num,book_list.count(),request) # 當前頁碼、總頁數、request book_list=book_list[pagination.start:pagination.end] # pagination.start能夠直接獲得開始頁的值 return render(request,"index.html",locals())
二、建立 Pagination 類用於分頁展現(app01中建立 page.py 用來成名分頁的類)
class Pagination(object): def __init__(self,current_page_num,all_count,request,per_page_num=10,pager_count=11,): #默認參數能夠改變 """ 封裝分頁相關數據 :param current_page_num: 當前訪問頁的數字 :param all_count: 分頁數據中的數據總條數 :request:裏面request.GET中含有get請求發送的鍵值對 :param per_page_num: 每頁顯示的數據條數 :param pager_count: 最多顯示的頁碼個數 """ try: current_page_num = int(current_page_num) except Exception as e: current_page_num = 1 if current_page_num <1: current_page_num = 1 self.current_page_num = current_page_num self.all_count = all_count self.per_page_num = per_page_num # 實際總頁碼 all_pager, tmp = divmod(all_count, per_page_num) if tmp: all_pager += 1 self.all_pager = all_pager self.pager_count = pager_count self.pager_count_half = int((pager_count - 1) / 2) # 5 # 保存搜索條件 import copy self.params=copy.deepcopy(request.GET) # {"a":"1","b":"2"} @property def start(self): return (self.current_page_num - 1) * self.per_page_num @property def end(self): return self.current_page_num * self.per_page_num # 該方法用於在頁面渲染分頁的標籤按鈕 def page_html(self): # 若是總頁碼 < 11個: if self.all_pager <= self.pager_count: pager_start = 1 pager_end = self.all_pager + 1 # 總頁碼 > 11 else: # 當前頁若是<=頁面上最多顯示11/2個頁碼 if self.current_page_num <= self.pager_count_half: pager_start = 1 pager_end = self.pager_count + 1 # 當前頁大於5 else: # 頁碼翻到最後 if (self.current_page_num + self.pager_count_half) > self.all_pager: pager_start = self.all_pager - self.pager_count + 1 pager_end = self.all_pager + 1 else: pager_start = self.current_page_num - self.pager_count_half pager_end = self.current_page_num + self.pager_count_half + 1 page_html_list = [] # 存放分頁標籤,用於總體渲染 # 首頁 self.params["page"]=1 # 添加鍵值對,page=1 first_page = '<li><a href="?%s">首頁</a></li>' % (self.params.urlencode(),) page_html_list.append(first_page) # 上一頁 self.params["page"]=self.current_page_num - 1 if self.current_page_num <= 1: prev_page = '<li class="disabled"><a href="#">上一頁</a></li>' else: prev_page = '<li><a href="?%s">上一頁</a></li>' % (self.params.urlencode(),) page_html_list.append(prev_page) # 中間頁 for i in range(pager_start, pager_end): self.params["page"]=i if i == self.current_page_num: temp = '<li class="active"><a href="?%s">%s</a></li>' %(self.params.urlencode(),i) else: temp = '<li><a href="?%s">%s</a></li>' % (self.params.urlencode(),i,) # 保存搜索條件 page_html_list.append(temp) # 下一頁 self.params["page"]=self.current_page_num + 1 if self.current_page_num >= self.all_pager: next_page = '<li class="disabled"><a href="#">下一頁</a></li>' else: next_page = '<li><a href="?%s">下一頁</a></li>' % (self.params.urlencode(),) page_html_list.append(next_page) # 尾頁 self.params["page"]=self.all_pager last_page = '<li><a href="?%s">尾頁</a></li>' % (self.params.urlencode(),) page_html_list.append(last_page) return ''.join(page_html_list)
三、頁面的渲染(html)
藉助bootstrap的樣式
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <!-- 渲染數據庫的內容 --> <ul> {% for book in book_list %} <li>{{ book.title }} ---- {{ book.price }}</li> {% endfor %} </ul> <!-- 生成分頁的標籤 藉助bootstrap的樣式 nav ul--> <nav aria-label="Page navigation"> <ul class="pagination"> {{ pagination.page_html|safe }} </ul> </nav> </body> </html>
class Pagination(object): """ 自定義分頁 """ def __init__(self,current_page,total_count,base_url,params,per_page_count=10,max_pager_count=11): try: current_page = int(current_page) except Exception as e: current_page = 1 if current_page <=0: current_page = 1 self.current_page = current_page # 數據總條數 self.total_count = total_count # 每頁顯示10條數據 self.per_page_count = per_page_count # 頁面上應該顯示的最大頁碼 max_page_num, div = divmod(total_count, per_page_count) if div: max_page_num += 1 self.max_page_num = max_page_num # 頁面上默認顯示11個頁碼(當前頁在中間) self.max_pager_count = max_pager_count self.half_max_pager_count = int((max_pager_count - 1) / 2) # URL前綴 self.base_url = base_url # request.GET import copy params = copy.deepcopy(params) params._mutable = True # 包含當前列表頁面全部的搜索條件 # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} # self.params[page] = 8 # self.params.urlencode() # source=2&status=2&gender=2&consultant=1&page=8 # href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8" # href="%s?%s" %(self.base_url,self.params.urlencode()) self.params = params @property def start(self): return (self.current_page - 1) * self.per_page_count @property def end(self): return self.current_page * self.per_page_count def page_html(self): # 若是總頁數 <= 11 if self.max_page_num <= self.max_pager_count: pager_start = 1 pager_end = self.max_page_num # 若是總頁數 > 11 else: # 若是當前頁 <= 5 if self.current_page <= self.half_max_pager_count: pager_start = 1 pager_end = self.max_pager_count else: # 當前頁 + 5 > 總頁碼 if (self.current_page + self.half_max_pager_count) > self.max_page_num: pager_end = self.max_page_num pager_start = self.max_page_num - self.max_pager_count + 1 #倒這數11個 else: pager_start = self.current_page - self.half_max_pager_count pager_end = self.current_page + self.half_max_pager_count page_html_list = [] # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} # 首頁 self.params['page'] = 1 first_page = '<li><a href="%s?%s">首頁</a></li>' % (self.base_url,self.params.urlencode(),) page_html_list.append(first_page) # 上一頁 self.params["page"] = self.current_page - 1 if self.params["page"] < 1: pervious_page = '<li class="disabled"><a href="%s?%s" aria-label="Previous">上一頁</span></a></li>' % ( self.base_url, self.params.urlencode()) else: pervious_page = '<li><a href = "%s?%s" aria-label = "Previous" >上一頁</span></a></li>' % ( self.base_url, self.params.urlencode()) page_html_list.append(pervious_page) # 中間頁碼 for i in range(pager_start, pager_end + 1): self.params['page'] = i if i == self.current_page: temp = '<li class="active"><a href="%s?%s">%s</a></li>' % (self.base_url,self.params.urlencode(), i,) else: temp = '<li><a href="%s?%s">%s</a></li>' % (self.base_url,self.params.urlencode(), i,) page_html_list.append(temp) # 下一頁 self.params["page"] = self.current_page + 1 if self.params["page"] > self.max_page_num: self.params["page"] = self.current_page next_page = '<li class="disabled"><a href = "%s?%s" aria-label = "Next">下一頁</span></a></li >' % ( self.base_url, self.params.urlencode()) else: next_page = '<li><a href = "%s?%s" aria-label = "Next">下一頁</span></a></li>' % ( self.base_url, self.params.urlencode()) page_html_list.append(next_page) # 尾頁 self.params['page'] = self.max_page_num last_page = '<li><a href="%s?%s">尾頁</a></li>' % (self.base_url, self.params.urlencode(),) page_html_list.append(last_page) return ''.join(page_html_list)
""" 自定義分頁組件的使用方法: pager_obj = Pagination(request.GET.get('page',1),len(HOST_LIST),request.path_info,request.GET) host_list = HOST_LIST[pager_obj.start:pager_obj.end] html = pager_obj.page_html() return render(request,'hosts.html',{'host_list':host_list,"page_html":html}) """
#批量建立數據 # booklist = [] # for i in range(100): # booklist.append(models.Book(name="book" + str(i), price=20 + i * i)) # # models.Book.objects.bulk_create(booklist)