目錄css
settings.pyhtml
LANGUAGE_CODE = 'zh-hans'
修改結果
python
應用/apps.pygit
from django.apps import AppConfig class BbssConfig(AppConfig): name = 'bbs' # 添加下面這句 verbose_name = 'BBS系統'
修改結果
github
應用/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) # 添加文件類型字段,並默認爲空 pub_date = models.DateTimeField(auto_now_add=True) def get_comment_text_md(self): """將markdown格式轉化爲html""" return mark_safe(markdown(self.comment_text)) def __str__(self): return self.comment_text class Meta: verbose_name = '評論' # 單數時顯示內容 verbose_name_plural = '評論' # 複數時顯示內容
默認數據庫表在後臺中顯示都爲複數形式,而中文沒有複數形式,所以將兩種形式都設置爲相同名稱django
修改結果
markdown
應用/models.pysession
class Comment(models.Model): topic = models.ForeignKey(Topic, on_delete=models.CASCADE, verbose_name='話題') comment_text = models.TextField('評價內容', max_length=2000) author = models.ForeignKey(User, default=1, on_delete=models.CASCADE, verbose_name='用戶') picture = models.FileField('圖片', blank=True, null=True) # 添加文件類型字段,並默認爲空 pub_date = models.DateTimeField('發佈時間', auto_now_add=True) def get_comment_text_md(self): """將markdown格式轉化爲html""" return mark_safe(markdown(self.comment_text)) def __str__(self): return self.comment_text class Meta: verbose_name = '評論' # 單數時顯示內容 verbose_name_plural = '評論' # 複數時顯示內容
通常的字段只需加個顯示名稱的位置參數就能夠,而一對多關係的要指定關鍵字參數 verbose_name
,而且關鍵字參數要放在位置參數後面app
修改結果
使用 django-grappelli 第三方應用進行修改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', ]
項目 / urls.py
urlpatterns = [ path('grappelli', include('grappelli.urls')), path('admin/', admin.site.urls), path('', include('bbs.urls')), path('accounts/', include('accounts.urls')), ]
settings.py 中添加
# 收集靜態文件統一存放的根路徑 STATIC_ROOT = os.path.join(BASE_DIR, 'static-files')
執行命令
python manage.py collectstatic
自動生成
再次啓動服務會發現管理頁面已經被修改
settings.py 中添加
# 後臺自定義標題 GRAPPELLI_ADMIN_TITLE = 'Z-BBS ADMIN'
刷新頁面
應用 / 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
應用 / 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
開啓以後記得強制刷新頁面(ctrl + shift + r),從新加載 js 和 css 代碼