先看效果圖:
登陸admin后的界面:
查看作者:
當(dāng)然你也可以定制admin, 使界面更牛逼
數(shù)據(jù)庫表結(jié)構(gòu): app01/models.py
1 from django.db import models 2 from django.utils.html import format_html #把字符串變成html 3 4 # Create your models here. 5 class Author(models.Model): 6 first_name = models.CharField(max_length=32) 7 last_name = models.CharField(max_length=32) 8 email = models.EmailField() 9 10 def __str__(self):11 return "<%s %s>" % (self.first_name,self.last_name)12 13 class Meta:14 verbose_name_plural = u"作者"15 16 class Publisher(models.Model):17 name = models.CharField(max_length=64, unique=True)18 address = models.CharField(max_length=128,null=True,blank=True)19 city = models.CharField(max_length=64)20 state_province = models.CharField(max_length=64,help_text="put your province here",verbose_name=u"所屬省")21 country = models.CharField(max_length=64,editable=False)22 website = models.URLField()23 24 def __str__(self):25 return "<%s>" % (self.name)26 27 class Book(models.Model):28 name = models.CharField(max_length=128)29 authors = models.ManyToManyField(Author) #書多對多到作者30 publisher = models.ForeignKey(Publisher) #一對多31 publish_date = models.DateField() #精確到天DateField()32 status_choices = (("published", u"已出版"),33 ("producing", u"待出版"),34 ("forbidden", u"禁書"),35 )36 # 此時應(yīng)該給個默認(rèn)值,給之前沒有status字段的書。37 status = models.CharField(choices=status_choices, max_length=32, default="producing")38 39 def __str__(self):40 return "<%s %s>" % (self.name,self.publisher)41 42 43 def colored_status(self):44 if self.status == "published":45 format_td = format_html("<span style='padding:2px;background-color:yellowgreen'>已出版</span>")46 elif self.status == "producing":47 format_td = format_html("<span style='padding:2px;background-color:red'>待出版</span>")48 elif self.status == "forbidden":49 format_td = format_html("<span style='padding:2px;background-color:yellow'>禁書</span>")50 51 return format_td52 53 colored_status.short_description = "STATUS" #頁面顯示為colored_status即方法名,這里改名為STATUS
現(xiàn)在你看到我直接貼代碼,肯定不爽。好吧……
首先上面有三張表: Author(作者表)、Publisher(出版社表)、Book(圖書表)。表與表的關(guān)聯(lián)也很簡單,一本書可由多個作者聯(lián)合出,一個作者可出多本書,書與作者為ManyToMany關(guān)系;一本書只能由一個出版社出版,一個出版社可出多本書,一對多,用外鍵ForeignKey關(guān)聯(lián)。
其它的__str__是啥?verbose_name是毛線東西? 看我之前的博客咯。
你登陸admin后其實是看不到3張表的,需要在admin.py注冊一下:
1 from app01 import models2 3 admin.site.register(models.Author)4 admin.site.register(models.Book, BookAdmin)5 admin.site.register(models.Publisher)
注冊后再刷新頁面,便可以看見表了。
一、form表單定制
在django進階-1已經(jīng)有下面這張圖了,當(dāng)你輸入為空時,django會出現(xiàn)下面的error提示。
但現(xiàn)在你前端玩得很6,你覺得django自己的form表單so uglily,你想自己做個form表單(可用bootstrap美化),但又希望你做的表單有django的驗證功能(重要!!)。so, How to do it?? 下面來自定制創(chuàng)建圖書的form表單。
第一步: 創(chuàng)建form類
你定制的form表單可能有很多,應(yīng)該建一個forms.py文件來存放定制的表單。
接下來在forms.py導(dǎo)入forms, 創(chuàng)建BookForm類(也可以命名為別的~)
1 from django import forms2 from app01 import models3 4 class BookForm(forms.Form):5 name = forms.CharField(max_length=10)6 # publisher_id = forms.IntegerField(widget=forms.Select)7 publish_date = forms.DateField()
第二步: 處理用戶請求
用戶最開始請求數(shù)據(jù)是GET方式, 當(dāng)輸入數(shù)據(jù)后修改數(shù)據(jù)(創(chuàng)建圖書),提交方式為POST
1 def book_form(request): 2 form = forms.BookForm() # 生成form實例 3 if request.method == "POST": 4 print(request.POST) 5 form = forms.BookForm(request.POST) 6 if form.is_valid(): #進行驗證 7 print("form is ok") 8 print(form) 9 print(form.cleaned_data) #{'name': 'qq', 'publish_date': datetime.date(2017, 3, 14)}10 form_data = form.cleaned_data11 form_data["publisher_id"] = request.POST.get("publisher_id")12 book_obj = models.Book(**form_data) #form_data為字典形式13 book_obj.save()14 else:15 print(form.errors)16 publisher_list = models.Publisher.objects.all()17 18 19 return render(request, "app01/book_form.html", {"book_form":form,20 "publishers":publisher_list})
當(dāng)用戶發(fā)起GET請求時,要生成一個空的form實例,并從數(shù)據(jù)庫中查找所有的出版社publisher_list,之后進行渲染render. 接下來當(dāng)然得寫book_form.html啦。
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <form action="" method="POST">{% csrf_token %} 9 {{ book_form }}10 <select name="publisher_id">11 {% for publisher in publishers %}12 <option value="{{ publisher.id }}">{{ publisher.name }}</option>13 {% endfor %}14 </select>15 <input type="submit" value="創(chuàng)建圖書"/>16 </form>17 </body>18 </html>
要是上面看不懂了,就別往下看了……可以看下我之前的博客哈。根據(jù)上面的html,會出現(xiàn)如下界面:
當(dāng)你輸入書名等信息,再點創(chuàng)建圖書時,此時是以POST方式發(fā)起請求。
我們要將數(shù)據(jù)發(fā)給django強大的form表單進行驗證(你可能沒有輸入或者日期輸入為SB), 這些屬于前端的驗證,前端的驗證是為了減輕后臺服務(wù)器的壓力。只需將請求的數(shù)據(jù)request.POST傳給forms.py的BookForm()即可。
1 | form = forms.BookForm(request.POST) |
此時我們還需要進行后臺驗證,有可能成功,also可能失敗。當(dāng)驗證成功時,form.is_valid返回true.
1 | if form.is_valid(): #進行驗證 |
驗證成功后,自然是獲取數(shù)據(jù),存到數(shù)據(jù)庫創(chuàng)建圖書啦。好,信息是封裝在form實例的,現(xiàn)打印form:
View Code
擦,一堆html
要獲取干凈的數(shù)據(jù),需處理一下:
1 | form_data = form.cleaned_data |
View Code
form表單定制ending.
這里有個問題,為啥要在BookForm去掉publisher_id字段??
因為無法直接獲取出版社的信息(下拉列表為空的),要想在前端界面有供選擇出版社的input標(biāo)簽,需要自己在html添加
View Code
最后還需要將獲得的信息加到字典(干凈的數(shù)據(jù))中,再添加到數(shù)據(jù)庫。
呵呵,這比較麻煩,下面介紹更優(yōu)化的方法。
modelform
為了解決前面無法獲取publisher_id的問題,引入modelform
modelform與form有很多相似之處的。接下來用modelform做個前端表單,當(dāng)然,會丑點……
第一步:
在forms.py創(chuàng)建類,繼承forms.ModelForm:
可綁定Book表,Book表有什么字段,form表單就有什么字段,當(dāng)然你也可以用fields使想顯示的字段顯示在界面。
1 class BookModelForm(forms.ModelForm): 2 3 class Meta: 4 model = models.Book #綁定Book類 5 #fields = {"name", "publish_date"} #只包括name,publish-date字段 6 exclude = () #代表所有字段都包括 7 widgets = { 8 # 可以定義css樣式,牛逼啊 9 "name": forms.TextInput(attrs={"class":"form-control"}),10 }
第二步: 處理用戶請求
1 def book_modelform(request): 2 form = forms.BookModelForm 3 print(request.POST) 4 if request.method == "POST": 5 form = forms.BookModelForm(request.POST) 6 if form.is_valid(): # 進行驗證 7 print("form is ok") 8 print(form) 9 print(form.cleaned_data) # {'name': 'qq', 'publish_date': datetime.date(2017, 3, 14)}10 form.save()11 12 return render(request, "app01/book_modelform.html", {"book_form":form})
第三步: 寫html模版,book_modelform.html:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <style> 7 .form-control{ 8 color: red; 9 }10 </style>11 </head>12 <body>13 <form action="" method="post">{% csrf_token %}14 {{ book_form }}15 <input type="submit" value="創(chuàng)建新書"/>16 </form>17 18 </body>19 </html>
效果圖:
app01/forms.py
View Code
二、admin定制
費話不多說,先上效果圖:
第一次看到這界面效果,臥槽,amazing....
django的admin后臺可以定制成上面的形式,當(dāng)然你也可以自己定制得更漂亮。
在admin.py加上一個繼承admin.ModelAdmin的類
1 class BookAdmin(admin.ModelAdmin): #定制book,需將類當(dāng)作參數(shù)傳給admin 2 # list_display不能顯示多對多的,比如authors: 3 # (admin.E109) The value of 'list_display[2]' must not be a ManyToManyField. 4 list_display = ("id","name", "publisher","publish_date","colored_status","status") #定制為三列 5 6 search_fields = ("name", "publisher__name") #__表示出版社關(guān)聯(lián)到名字 7 list_filter = ("publisher", "publish_date") #過濾 8 list_editable = ("name", "publish_date") #可修改 9 list_per_page = 10 #每頁顯示數(shù)量10 #即便只有一個值也要加逗號,不然就不當(dāng)成元組了11 #The value of 'filter_horizontal' must be a list or tuple.12 filter_horizontal = ("authors",) #選擇作者時進行定制 PS:用于多對多關(guān)聯(lián)13 raw_id_fields = ("publisher",) #選擇出版社時進行定制 PS:用于FK外鍵關(guān)聯(lián)14 15 actions = [make_forbidden]
定制后完,需將類當(dāng)作參數(shù)傳給admin:
1 | admin.site.register(models.Book, BookAdmin) |
定制作者與出版社的效果圖:
ok, 現(xiàn)在有個新需求,想在book界面有個可以選擇書出版或未出版的功能,如何實現(xiàn)呢? 而且可以給界面的已出版/未出版加上顏色樣式??
首先你要給book表加上一個新的字段:
1 status_choices = (("published", u"已出版"),2 ("producing", u"待出版"),3 ("forbidden", u"禁書"),4 )5 # 此時應(yīng)該給個默認(rèn)值,給之前沒有status字段的書。6 status = models.CharField(choices=status_choices, max_length=32, default="producing")7 8 def __str__(self):9 return "<%s %s>" % (self.name,self.publisher)
再將status字段加到list_display,就能顯示在界面上了。so easy.
如何實現(xiàn)上圖的顏色呢??
在models.py下的Book類(用于創(chuàng)建Book表),定義colored_status方法(也可定義為別的名~) 加入下面代碼:
1 def colored_status(self): 2 if self.status == "published": 3 format_td = format_html("<span style='padding:2px;background-color:yellowgreen'>已出版</span>") 4 elif self.status == "producing": 5 format_td = format_html("<span style='padding:2px;background-color:red'>待出版</span>") 6 elif self.status == "forbidden": 7 format_td = format_html("<span style='padding:2px;background-color:yellow'>禁書</span>") 8 9 return format_td10 11 colored_status.short_description = "STATUS" #頁面默認(rèn)顯示為colored_status即方法名,這里改名為STATUS
自我感覺注釋得挺明白的。對不同的status判斷,并返回不同的css樣式。
這里注意導(dǎo)入format_html,可將字符串變成對應(yīng)html
1 | from django.utils.html import format_html #把字符串變成html |
admin action
需求又來了……
批量修改書的status,如將選中所有的書全都修改為禁書。
上圖的Action下拉框默認(rèn)只有Delete selected books,如何才能使下拉框能Set to forbidden選項?? 實現(xiàn)選擇forbidden再點擊Go,便可將選中的書的status改為禁書。
第一步: 在admin.py定義如下方法,方法名你隨意。
要使界面下拉框有Set to forbidden選項,可用第5句代碼:
1 def make_forbidden(modelAdmin, request, queryset): #queryset:選中的集合;modelAdmin代表BookAdmin類,相當(dāng)于self2 print("----->", modelAdmin,request,queryset)3 queryset.update(status="forbidden") #更改選中的為禁書4 #使action框有選項set to forbidden5 make_forbidden.short_description = "Set to forbidden"
第二步: 在之前定義的BookAdmin類加上actions=[xx], xx為第一步定義的方法名make_forbidden.
1 2 | class BookAdmin(admin.ModelAdmin): #在類中加入下面一句代碼. actions = [make_forbidden] |
admin.py
View Code
可別小看admin action, 比如你可以批量選中很多主機,點擊action發(fā)送指令,發(fā)送文件 and so on. 強!!!
轉(zhuǎn)發(fā)注明出處: http://www.cnblogs.com/0zcl/p/6580279.html
作者:前程明亮
出處:http://www.cnblogs.com/0zcl
文章未標(biāo)明轉(zhuǎn)載則為原創(chuàng)博客。歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利.
1.非系統(tǒng)的學(xué)習(xí)也是在浪費時間
2.做一個會欣賞美,懂藝術(shù),會藝術(shù)的技術(shù)人
http://www.cnblogs.com/0zcl/p/6580279.html