django (三) admin后臺系統

admin后臺系統

1. 安裝MySQL

1,安裝mysql:
    sudo apt install mysql-server
 ?  (安裝過程中輸入密碼并牢記)
 ?  
2,安裝后進入mysql:
    mysql -u用戶名 -p密碼
    mysql -uroot -proot
?
3,在Django中配置和使用mysql數據庫
使用mysql數據庫,settings中配置如下:
    DATABASES = {
     ?  'default': {
     ? ? ?  'ENGINE': 'django.db.backends.mysql',
     ? ? ?  'NAME': 'mydb',
     ? ? ?  'USER': 'root',
     ? ? ?  'PASSWORD': 'root',
     ? ? ?  'HOST': '127.0.0.1',
     ? ? ?  'PORT': '3306',
     ?  }
    }
4, 添加PyMySQL 
 然后使用pip添加依賴包: pip install PyMySQL
 并在工程目錄下的__init__.py中添加以下代碼來配置PyMySQL: 
    import pymysql
    pymysql.install_as_MySQLdb()

2. django admin后臺系統

Django中默認集成了后臺數據管理頁面,通過簡單的配置就可以實現模型后臺的Web控制臺。
管理界面通常是給系統管理員使用的,用來完成數據的輸入,刪除,查詢等工作。
使用以下models來示范admin后臺系統的用法。
?
創建一個項目, 用來說明出版社, 書籍和作者的關系。
 ? 假定關系:作者:書籍 => 1:n  (一本書由一個作者完成, 一個作者可以創作多本書)
          出版社:書籍 => n:n (一個出版社可以出版多本書, 一本書可以由多個出版社出版)
?
要求:
 ?  1. 創建作者author, 出版社publisher,書籍book三個應用.
 ?  2. 給每個應用分別創建首頁index.html,且可以在瀏覽器打開index頁面.
 ?  3. 在書籍的index.html中有一個"查看所有書籍"的超鏈接按鈕,點擊進入書籍列表list.html頁面.
 ?  4. 在書籍list.html中顯示所有書名,點擊書名可以進入書籍詳情detail.html
 ?  5. 在書籍detail.html中可以點擊該書的作者和出版社,進入作者的detail.html和出版社的detail.html頁面
?
models.py內容如下:
    # 出版社
    class Publisher(models.Model):
 ? ? ?  name = models.CharField(max_length=30)
 ? ? ?  address = models.CharField(max_length=100)
 ? ? ?  city = models.CharField(max_length=30)
 ? ? ?  state_province = models.CharField(max_length=30)
 ? ? ?  country = models.CharField(max_length=20)
 ? ? ?  website = models.URLField()
 ? ? ?  
    # 作者
    class Author(models.Model):
 ? ? ?  first_name = models.CharField(max_length=30)
 ? ? ?  last_name = models.CharField(max_length=30)
 ? ? ?  email = models.EmailField()
 ? ? ?  gender = models.BooleanField(default=True)
?
    # 書籍
    class Book(models.Model):
 ? ? ?  title = models.CharField(max_length=100)
 ? ? ?  author = models.ForeignKey(Author)
 ? ? ?  publishers = models.ManyToManyField(Publisher)
 ? ? ?  publish_date = models.DateField()
?
使用admin后臺系統之前,需要先創建一個系統管理員,創建管理員之前需先同步數據庫。
    python manager.py createsuperuser
設置為中文
    settingsLANGUAGE_CODE = 'zh-hans'
設置時間,時區
    TIME_ZONE='Asia/Shanghai'
?
添加自己的數據模型,admin.py中注冊: 
    admin.site.register(Publisher)
    admin.site.register(Author)
    admin.site.register(Book)
admin中給model添加數據。
給模型加上__str__函數,比如給Author模型添加str函數,讓author的顯示更加友好:
    def __str__(self):
 ?      return '%s %s' % (self.first_name, self.last_name)
?
希望控制admin中添加model數據時的動作,可以修改相應字段的屬性。
比如authoremail字段運行添加的時候為空,可以在email字段定義中加上 blank=True(可以空白),
比如bookpublication_date添加 blank=True, null=True(可以為null)屬性。
修改models屬性之后記得及時做數據遷移。
?
使用verbose_name屬性指定字段的別名:
 ?  比如給publishername字段指定一個中文的別名verbose_name='出版社名稱'
    models的修改頁面,默認顯示的是models定義的str函數返回的字符串。
 ? ? ? ? ?  

3. 定制admin

通過定義MoldelAdmin來定制modeladmin的表現。比如給Author定義AuthorAdmin
    class AuthorAdmin(admin.ModelAdmin):
        list_display = ('first_name', 'last_name', 'email')
    相應的注冊代碼也要變化:
    admin.site.register(Author, AuthorAdmin)
?
Author添加一個搜索框:
    search_fields = ('first_name', 'last_name')
book添加一個過濾器
    list_filter = ('publication_date',)
    過濾器不光可以作用在日期字段上,還可以作用在boolean類型和外鍵上。
    另一種增加日期過濾的方式:
    date_hierarchy = 'publication_date'
字段排序:
    ordering = ('-publication_date',)
 
修改編輯頁面顯示的字段及顯示順序,默認按照models中字段的定義順序顯示:
    fields = ('title', 'authors', 'publisher', 'publication_date')
fields相反的字段是exclude
    exclude = ['publication_date',] 
改善多對多關系中對象選擇操作,比如給BookAdmin添加如下屬性:
    filter_horizontal = ('authors',)
filter_horizontalfilter_vertical 選項只適用于多對多關系。
?
一對多的外鍵關系,admin使用select box下拉菜單來表示。如不想用select box,可添加如下屬性,讓原來一次性加載所有publisherselect box變成填寫publisherid
    raw_id_fields = ('publisher',)
?
讓字段分組顯示,fieldsets和上面提到的field不能同時出現:
    fieldsets = (
 ?      ('作者', {'fields': ('authors',)}),
 ?      ('出版商', {'fields': ('publisher',)}),
    )
?
定制list_display字段的顯示。比如給Author加一個布爾型gender字段,來表示性別。為了讓顯示更加人性化:
    # 定制顯示屬性
 ?  def showgender(self):
 ? ? ?  if self.gender:
 ? ? ? ? ?  return '男'
 ? ? ?  else:
 ? ? ? ? ?  return '女'
 ?  list_display = ('first_name', 'last_name', 'email', showgender)
給該函數設置簡短描述,讓顯示更加友好:
    showgender.short_description = '性別'
?
可以將modeladmin的屬性簡單劃分為列表頁屬性和添加、修改頁屬性
?
# 列表頁屬性
list_display,list_filter,search_fields,list_per_page
?
# 添加、修改頁屬性
fields ,fieldsets, filter_horizontal, raw_id_fields

轉載于:https://www.cnblogs.com/gugubeng/p/9723360.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279259.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279259.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279259.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python博客訪問量_史詩級干貨-python爬蟲之增加CSDN訪問量

AI人工智能史詩級干貨-python爬蟲之增加CSDN訪問量史詩級干貨-python爬蟲之增加CSDN訪問量搜索微信公眾號:‘AI-ming3526’或者’計算機視覺這件小事’ 獲取更多算法、機器學習干貨csdn:https://blog.csdn.net/baidu_31657889/github:https://github.com…

弄斷過河電纜_你說的是:剪斷電纜線

弄斷過河電纜Earlier this week we asked you if you’d cut the cable and switched to alternate media sources to get your movie and TV fix. You responded and we’re back with a What You Said roundup. 本周早些時候,我們問您是否要切斷電纜并切換到其他媒…

復制粘貼的句子

Today you do things people will not do,tomorrow you will do things people can not do. 你今天做別人不愿做的事,明天就能做別人做不到的事。轉載于:https://www.cnblogs.com/wensens/p/9723998.html

路由銷毀上一頁_路由器原理(數據通信)

路由:對數據包選擇路徑的過程路由器(也叫網關)智能選擇數據傳輸路由的設備,其端口數量較少!功能:連接網絡1.連接異構網絡以太網、ATM網絡、FDDI網絡2.連接遠程網絡局域網、廣域網隔離廣播將廣播隔離在局域網內路由選擇網絡安全地址…

您可能沒有使用的最佳三星Galaxy功能

Samsung packs its flagship phones with a slew of features—some are even better than stock Android. Either way, there are a lot of things on these phones that you may not be using. Here are some of the best. 包三星旗艦手機用的特性-擺有的甚至比普通的Android…

win7更新錯誤0x800b0109_win7更新漏洞后產生0x0000006B藍屏的解決方法圖解

這幾天不少網友在使用win7更新補丁后就藍屏了,代碼為0x0000006b。發生這一藍屏問題的都是安裝了2016年四月份推出的安全更新補丁,安裝后就出現藍屏,有的網友表示沒問題,有的直接藍了。這個藍屏重啟后依舊,安全模式進不…

獲取構造器的信息

獲取類構造器的用法與上述獲取方法的用法類似,如: import java.lang.reflect.*;public class constructor1 {public constructor1() {}protected constructor1(int i, double d) { } public static void main(String args[]) { try { Class cls Class.f…

如何使用facebook_如果每個人都已經開始使用Facebook,Facebook能否繼續發展?

如何使用facebookThere are only so many people on earth, and so many hours in the day. Is that starting to limit the growth of social media? 地球上只有那么多人,一天中有很多小時。 這是否開始限制社交媒體的增長? Think about how much time…

2018-10-03-Python全棧開發-day60-django序列化-part3

聯合唯一 clean_字段方法只能對某個字段進行檢查,當clean方法執行完之后,最后還會執行clean方法,在clean方法中,可以通過獲取數據字典中的值然后進行驗證 from django.shortcuts import render,HttpResponsefrom django import fo…

mysql時間字段條件查詢_mysql 查詢 時間作為查詢條件

今天select * from 表名 where to_days(時間字段名) to_days(now());昨天SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 時間字段名) < 1近7天SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) < date(時間字段名)近30天SELECT * FROM 表名 whe…

mac按文件名查找文件_如何在Mac上查找和刪除大文件

mac按文件名查找文件Freeing up disk space on a full hard drive can be difficult, especially when it’s full of small files. However, there are some excellent tools for macOS that let you find the files taking up the most space and delete the ones you don’t…

Swift5.1 語言參考(十) 語法匯總

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★?微信公眾號&#xff1a;山青詠芝&#xff08;shanqingyongzhi&#xff09;?博客園地址&#xff1a;山青詠芝&#xff08;https://www.cnblogs.com/strengthen/&#xff09;?GitHub地址&a…

timestamp mysql php_PHP和Mysql的Timestamp互換

在mysql中有三種時間字段類型&#xff1a;DATETIME&#xff0c;DATE和TIMESTAMP。DATETIME以YYYY-MM-DD HH:MM:SS格式的字符串來保存數據&#xff1b;DATE則是只有年月日以YYYY-MM-DD形式的字串&#xff1b;TIMESTAMP類型和PHP中的TIMESTAMP類型名字一樣&#xff0c;但是兩者基…

dmg是什么文件格式_什么是DMG文件(以及我該如何使用)?

dmg是什么文件格式DMG files are containers for apps in macOS. You open them, drag the app to your Applications folder, and then eject them, saving you the hassle of the dreaded “Install Wizard” of most Windows apps. So if all they are is a folder for an a…

mysql索引三個字段查詢兩個字段_mysql中關于關聯索引的問題——對a,b,c三個字段建立聯合索引,那么查詢時使用其中的2個作為查詢條件,是否還會走索引?...

情況描述&#xff1a;在MySQL的user表中&#xff0c;對a,b,c三個字段建立聯合索引&#xff0c;那么查詢時使用其中的2個作為查詢條件&#xff0c;是否還會走索引&#xff1f;根據查詢字段的位置不同來決定&#xff0c;如查詢a, a,b a,b,c a,c 都可以走索引的&#…

HDU 3966 Aragorn's Story (樹鏈剖分+線段樹)

題意&#xff1a;給你一棵樹&#xff0c;然后有三種操作 I L R K: 把L與R的路徑上的所有點權值加上K D L R K&#xff1a;把L與R的路徑上的所有點權值減去K Q X&#xff1a;查詢節點編號為X的權值 思路&#xff1a;樹鏈剖分裸題&#xff08;我還沒有怎么學懂&#xff0c;但基本…

canon相機api中文_您應該在佳能相機上掌握的10種相機設置

canon相機api中文Your camera is a tool, and you should be able to use it with total confidence. You should never have to dig through the manual or play around with random buttons trying to work out how to do something on a shoot. Here are the most important…

mysql普通索引自增_mysql中聯合索引中的自增列的增長策略

《深入理解MySQL》中一段介紹MyISAM存儲引擎中自動增長列的示例,如下1 mysql>create table autoincre_demo2 -> (d1 smallint not nullauto_increment,3 -> d2 smallint not null,4 -> name varchar(10),5 ->index(d2,d1)6 -> )enginemyisam;7 Query OK, 0 r…

spring-boot基礎概念與簡單應用

1.spring家族 2.應用開發模式 2.1單體式應用 2.2微服務架構 微服務架構中每個服務都可以有自己的數據庫 3.微服務架構應當注意的細節 3.1關于"持續集成,持續交付,持續部署" 頻繁部署、快速交付以及開發測試流程自動化都將成為未來軟件工程的重要組成部分 可行方案(如…

郵箱客戶端 gmail支持_如何聯系Gmail支持

郵箱客戶端 gmail支持Although you may not be able to directly contact Gmail support without subscribing to G Suite for businesses, there are a couple of ways to get the answers you’re looking for online. Let’s look at how you can get help with your Gmail …