您好,登錄后才能下訂單哦!
Django 中自定義 Admin 樣式的實(shí)現(xiàn)?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
settings.py
LANGUAGE_CODE = 'zh-hans'
修改結(jié)果
應(yīng)用/apps.py
from django.apps import AppConfig class BbssConfig(AppConfig): name = 'bbs' # 添加下面這句 verbose_name = 'BBS系統(tǒng)'
修改結(jié)果
應(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ù)時(shí)顯示內(nèi)容 verbose_name_plural = '評論' # 復(fù)數(shù)時(shí)顯示內(nèi)容
默認(rèn)數(shù)據(jù)庫表在后臺中顯示都為復(fù)數(shù)形式,而中文沒有復(fù)數(shù)形式,因此將兩種形式都設(shè)置為相同名稱
修改結(jié)果
應(yīng)用/models.py
class Comment(models.Model): topic = models.ForeignKey(Topic, on_delete=models.CASCADE, verbose_name='話題') comment_text = models.TextField('評價(jià)內(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ā)布時(shí)間', 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ù)時(shí)顯示內(nèi)容 verbose_name_plural = '評論' # 復(fù)數(shù)時(shí)顯示內(nèi)容
一般的字段只需加個(gè)顯示名稱的位置參數(shù)就可以,而一對多關(guān)系的要指定關(guān)鍵字參數(shù) verbose_name
,并且關(guān)鍵字參數(shù)要放在位置參數(shù)后面
修改結(jié)果
使用 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/
pip install django-grappelli
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', ]
項(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)一放到一個(gè)地方
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)被修改
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
應(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 代碼
看完上述內(nèi)容,你們掌握Django 中自定義 Admin 樣式的實(shí)現(xiàn)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。