Django 中自定義 Admin 樣式與功能的實(shí)現(xiàn)方法
自定義 Admin 樣式與功能
1 頁面修改中文
1.1 語言設(shè)置為中文
settings.py
LANGUAGE_CODE = 'zh-hans'
修改結(jié)果
1.2 應(yīng)用管理設(shè)置為中文
應(yīng)用/apps.py
from django.apps import AppConfig class BbssConfig(AppConfig): name = 'bbs' # 添加下面這句 verbose_name = 'BBS系統(tǒng)'
修改結(jié)果
1.3 數(shù)據(jù)庫表設(shè)置為中文
應(yīng)用/models.py
class Comment(models.Model): topic = models.ForeignKey(Topic, on_delete=models.CASCADE) comment_text = models.TextField(max_length=2000) author = models.ForeignKey(User, default=1, on_delete=models.CASCADE) picture = models.FileField(blank=True, null=True) # 添加文件類型字段,并默認(rèn)為空 pub_date = models.DateTimeField(auto_now_add=True) def get_comment_text_md(self): """將markdown格式轉(zhuǎn)化為html""" return mark_safe(markdown(self.comment_text)) def __str__(self): return self.comment_text class Meta: verbose_name = '評論' # 單數(shù)時顯示內(nèi)容 verbose_name_plural = '評論' # 復(fù)數(shù)時顯示內(nèi)容
默認(rèn)數(shù)據(jù)庫表在后臺中顯示都為復(fù)數(shù)形式,而中文沒有復(fù)數(shù)形式,因此將兩種形式都設(shè)置為相同名稱
修改結(jié)果
1.4 數(shù)據(jù)庫表字段名稱修改為中文
應(yīng)用/models.py
class Comment(models.Model): topic = models.ForeignKey(Topic, on_delete=models.CASCADE, verbose_name='話題') comment_text = models.TextField('評價內(nèi)容', max_length=2000) author = models.ForeignKey(User, default=1, on_delete=models.CASCADE, verbose_name='用戶') picture = models.FileField('圖片', blank=True, null=True) # 添加文件類型字段,并默認(rèn)為空 pub_date = models.DateTimeField('發(fā)布時間', auto_now_add=True) def get_comment_text_md(self): """將markdown格式轉(zhuǎn)化為html""" return mark_safe(markdown(self.comment_text)) def __str__(self): return self.comment_text class Meta: verbose_name = '評論' # 單數(shù)時顯示內(nèi)容 verbose_name_plural = '評論' # 復(fù)數(shù)時顯示內(nèi)容
一般的字段只需加個顯示名稱的位置參數(shù)就可以,而一對多關(guān)系的要指定關(guān)鍵字參數(shù) verbose_name
,并且關(guān)鍵字參數(shù)要放在位置參數(shù)后面
修改結(jié)果
2 修改后臺樣式
使用 django-grappelli 第三方應(yīng)用進(jìn)行修改admin樣式
GitHub:https://github.com/sehmaschine/django-grappelli
文檔:https://django-grappelli.readthedocs.io/en/latest/quickstart.html
其他工具:https://djangopackages.org/grids/g/admin-interface/
2.1 安裝
pip install django-grappelli
2.2 導(dǎo)入項(xiàng)目
settings.py
INSTALLED_APPS = [ 'accounts.apps.AccountsConfig', 'polls.apps.PollsConfig', 'bbs.apps.BbssConfig', 'grappelli', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
2.3 導(dǎo)入URL
項(xiàng)目 / urls.py
urlpatterns = [ path('grappelli', include('grappelli.urls')), path('admin/', admin.site.urls), path('', include('bbs.urls')), path('accounts/', include('accounts.urls')), ]
2.4 收集靜態(tài)文件統(tǒng)一放到一個地方
settings.py 中添加
# 收集靜態(tài)文件統(tǒng)一存放的根路徑 STATIC_ROOT = os.path.join(BASE_DIR, 'static-files')
執(zhí)行命令
python manage.py collectstatic
自動生成
再次啟動服務(wù)會發(fā)現(xiàn)管理頁面已經(jīng)被修改
2.5 自定義標(biāo)題
settings.py 中添加
# 后臺自定義標(biāo)題 GRAPPELLI_ADMIN_TITLE = 'Z-BBS ADMIN'
刷新頁面
2.6 admin開啟分頁功能
應(yīng)用 / admin.py
from django.contrib import admin # Register your models here. from .models import Topic, Comment class TopicAdmin(admin.ModelAdmin): list_display = ('topic_text', 'author', 'pub_date') search_fields = ('topic_text', 'author') list_editable = ('author',) list_per_page = 10 class CommentAdmin(admin.ModelAdmin): list_display = ( 'comment_text', 'author', 'pub_date', 'topic') search_fields = ('comment_text', 'author') list_editable = ('author',) list_per_page = 10
2.7 admin 開啟過濾功能
應(yīng)用 / admin.py
class TopicAdmin(admin.ModelAdmin): list_display = ('topic_text', 'author', 'pub_date') list_filter = ('topic_text', 'author', 'pub_date') search_fields = ('topic_text',) list_editable = ('author',) list_per_page = 10 class CommentAdmin(admin.ModelAdmin): list_display = ( 'comment_text', 'author', 'pub_date', 'topic') list_filter = ('comment_text', 'author', 'pub_date', 'topic') search_fields = ('comment_text',) list_editable = ('author',) list_per_page = 10
開啟之后記得強(qiáng)制刷新頁面(ctrl + shift + r),重新加載 js 和 css 代碼
總結(jié)
以上所述是小編給大家介紹的Django 中自定義 Admin 樣式與功能的實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Python的Django框架下管理站點(diǎn)的基本方法
這篇文章主要介紹了Python的Django框架下管理站點(diǎn)的基本方法,需是Django站點(diǎn)部署的基礎(chǔ),要的朋友可以參考下2015-07-07關(guān)于Series的index的方法和屬性使用說明
這篇文章主要介紹了關(guān)于Series的index的方法和屬性使用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06python scatter散點(diǎn)圖用循環(huán)分類法加圖例
這篇文章主要為大家詳細(xì)介紹了python scatter散點(diǎn)圖用循環(huán)分類法加圖例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03python實(shí)現(xiàn)把二維列表變?yōu)橐痪S列表的方法分析
這篇文章主要介紹了python實(shí)現(xiàn)把二維列表變?yōu)橐痪S列表的方法,結(jié)合實(shí)例形式總結(jié)分析了Python列表推導(dǎo)式、嵌套、循環(huán)等相關(guān)操作技巧,需要的朋友可以參考下2019-10-10用于業(yè)余項(xiàng)目的8個優(yōu)秀Python庫
今天小編就為大家分享一篇用于業(yè)余項(xiàng)目的8個大型Python庫,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-09-09Python 使用 consul 做服務(wù)發(fā)現(xiàn)示例詳解
這篇文章主要介紹了Python 使用 consul 做服務(wù)發(fā)現(xiàn)示例詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03