目錄html
若是咱們要在數據庫中成批量的插入數據,咱們有兩種方法,見下圖:前端
相對於第一種方法(進行for循環,把每一條數據一一插入到數據庫中),bulk_create
的插入數據的速度實在是太快了,和第一種真的不能相提並論,直接上圖,一決高下python
咱們只須要新建一個py文件,將代碼直接拷貝過去就行正則表達式
後端數據庫
前端django
自定義分頁器內容後端
class Pagination(object): def __init__(self, current_page, all_count, per_page_num=10, pager_count=11): """ 封裝分頁相關數據 :param current_page: 當前頁 :param all_count: 數據庫中的數據總條數 :param per_page_num: 每頁顯示的數據條數 :param pager_count: 最多顯示的頁碼個數 用法: queryset = model.objects.all() page_obj = Pagination(current_page,all_count) page_data = queryset[page_obj.start:page_obj.end] 獲取數據用page_data而再也不使用原始的queryset 獲取前端分頁樣式用page_obj.page_html """ try: current_page = int(current_page) except Exception as e: current_page = 1 if current_page < 1: current_page = 1 self.current_page = current_page 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) @property def start(self): return (self.current_page - 1) * self.per_page_num @property def end(self): return self.current_page * 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 <= self.pager_count_half: pager_start = 1 pager_end = self.pager_count + 1 # 當前頁大於5 else: # 頁碼翻到最後 if (self.current_page + self.pager_count_half) > self.all_pager: pager_end = self.all_pager + 1 pager_start = self.all_pager - self.pager_count + 1 else: pager_start = self.current_page - self.pager_count_half pager_end = self.current_page + self.pager_count_half + 1 page_html_list = [] # 添加前面的nav和ul標籤 page_html_list.append(''' <nav aria-label='Page navigation>' <ul class='pagination'> ''') first_page = '<li><a href="?page=%s">首頁</a></li>' % (1) page_html_list.append(first_page) if self.current_page <= 1: prev_page = '<li class="disabled"><a href="#">上一頁</a></li>' else: prev_page = '<li><a href="?page=%s">上一頁</a></li>' % (self.current_page - 1,) page_html_list.append(prev_page) for i in range(pager_start, pager_end): if i == self.current_page: temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i,) else: temp = '<li><a href="?page=%s">%s</a></li>' % (i, i,) page_html_list.append(temp) if self.current_page >= self.all_pager: next_page = '<li class="disabled"><a href="#">下一頁</a></li>' else: next_page = '<li><a href="?page=%s">下一頁</a></li>' % (self.current_page + 1,) page_html_list.append(next_page) last_page = '<li><a href="?page=%s">尾頁</a></li>' % (self.all_pager,) page_html_list.append(last_page) # 尾部添加標籤 page_html_list.append(''' </nav> </ul> ''') return ''.join(page_html_list)
優勢:django orm自動幫你建立第三張表,方便app
缺點:只會幫你建立兩個表的關係字段,不會額外添加額外的字段,第三張表的擴展性差函數
class BOook(model.Model): ... authors = models.ManyToManyField(to='Author') class Author(models.Model): ... books = models.ManyToManyField(to='Book') #自動建立的關係表中,有且只有兩個外鍵字段 class Book_Author(models.model) book author
優勢:第三張表能夠任意的添加額外的字段post
缺點:在orm查詢的時候,不少方法都不支持,很是麻煩
class Book(model.Model): ... authors = models.ManyToManyField(to='Author') class Author(models.Model): ... books = models.ManyToManyField(to='Book') class Book2Author(models.model) book = models.Foreignkey(to='Book') author = models.Foreignkey(to='Author')
手動建表,可是要告訴orm,第三張表示本身建的,只須要告訴orm給我提供方便的查詢方法
雖然可使用orm查詢方法,可是不支持使用add()
,set()
,remove
,clear()
關鍵參數through
,through_fields
不能忘記
class Book(model.Model): ... authors = models.ManyToManyField(to='Author',through='Book2Author',through_field=('book','author') ) class Author(models.Model): ... books = models.ManyToManyField(to='Book',through='Book2Author',through_fields=('author','book') class Book2Author(models.model) book = models.Foreignkey(to='Book') author = models.Foreignkey(to='Author') create_time = models.DateField(auto_now_add=True)
可以自動幫咱們渲染前端頁面、校驗數據、展現錯誤信息
forms組件只會幫你渲染獲取用戶輸入(輸入,選擇,下拉框...)的標籤 ,提交按鈕須要本身手動寫
後端代碼準備
封裝程度過高了,標籤樣式及參數不方便調整 可擴展性差(不推薦使用)
<p> {{ form_obj.as_p }} #被p標籤包裹 {{ form_obj.as_ul }} #被li標籤包裹 </p>
擴展性較高,可是須要手寫的代碼量比較多(不推薦使用)
<p> {{ form_obj.username.label }}{{ form_obj.username }} </p> <p> {{ form_obj.password.label }}{{ form_obj.password }} </p> <p> {{ form_obj.email.label }}{{ form_obj.email }} </p>
不管有多少字段,代碼量比較少,代碼之間的label
和form_obj
互相解耦,擴展性較高(推薦使用)
{% for foo in form_obj %} <p>{{ foo.label }}{{ foo }}</p> {% endfor %}
from django import forms class MyRegForm(forms.Form): username = forms.CharField(min_length=3,max_length=8) password = forms.CharField(min_length=3,max_length=8) email = forms.EmailField()
搭建測試環境以後,進行測試的結果
form
表單中添加一個參數便可
<form action="" method="post" novalidate>
這個widget
的類型能夠是widget.PasswordInput
等其它,若是是密碼格式,用戶輸入密碼的時候是隱藏的,不可見的。
from django import forms from django.core.validators import RegexValidato class MyRegForm(forms.Form): phone = forms.CharField(label='手機號',validators=[RegexValidator(r'^[0-9]+$', '請輸入數字'), RegexValidator(r'^159[0-9]+$', '數字必須以159開頭')])
gender = forms.ChoiceField( choices=((1, "男"), (2, "女"), (3, "保密")), label="性別", initial=3, widget=widgets.RadioSelect() ) hobby = forms.ChoiceField( choices=((1, "籃球"), (2, "足球"), (3, "雙色球"),), label="愛好", initial=3, widget=widgets.Select() ) hobby1 = forms.MultipleChoiceField( choices=((1, "籃球"), (2, "足球"), (3, "雙色球"),), label="愛好", initial=[1, 3], widget=widgets.SelectMultiple() ) keep = forms.ChoiceField( label="是否記住密碼", initial="checked", widget=forms.widgets.CheckboxInput() ) hobby2 = forms.MultipleChoiceField( choices=((1, "籃球"), (2, "足球"), (3, "雙色球"),), label="愛好", initial=[1, 3], widget=forms.widgets.CheckboxSelectMultiple()
當你須要對某一個字段數據進行額外的一些校驗,能夠考慮鉤子函數
針對單個字段數據 使用局部鉤子
就是說她已經在咱們剛纔校驗一邊的基礎上再進行局部的校驗,她已經經過了第一道門檻,這是第二道門檻
針對多個字段的校驗,可使用全局鉤子 eg:註冊功能的時候用戶的密碼要寫2次,校驗兩次密碼是否一致