溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Django 中自定義 Admin 樣式的實(shí)現(xiàn)

發(fā)布時(shí)間:2021-06-03 16:36:24 來源:億速云 閱讀:300 作者:Leah 欄目:開發(fā)技術(shù)

Django 中自定義 Admin 樣式的實(shí)現(xiàn)?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

1 頁面修改中文

1.1 語言設(shè)置為中文

settings.py

LANGUAGE_CODE = 'zh-hans'

修改結(jié)果


Django 中自定義 Admin 樣式的實(shí)現(xiàn)

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é)果


Django 中自定義 Admin 樣式的實(shí)現(xiàn)

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ù)時(shí)顯示內(nèi)容
  verbose_name_plural = '評論' # 復(fù)數(shù)時(shí)顯示內(nèi)容

默認(rèn)數(shù)據(jù)庫表在后臺中顯示都為復(fù)數(shù)形式,而中文沒有復(fù)數(shù)形式,因此將兩種形式都設(shè)置為相同名稱

修改結(jié)果


Django 中自定義 Admin 樣式的實(shí)現(xiàn)

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('評價(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 中自定義 Admin 樣式的實(shí)現(xiàn)

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)入U(xiǎn)RL

項(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

Django 中自定義 Admin 樣式的實(shí)現(xiàn)

自動生成

Django 中自定義 Admin 樣式的實(shí)現(xiàn)

再次啟動服務(wù)會發(fā)現(xiàn)管理頁面已經(jīng)被修改

Django 中自定義 Admin 樣式的實(shí)現(xiàn)

2.5 自定義標(biāo)題

settings.py 中添加

# 后臺自定義標(biāo)題
GRAPPELLI_ADMIN_TITLE = 'Z-BBS ADMIN'

刷新頁面

Django 中自定義 Admin 樣式的實(shí)現(xiàn)

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

Django 中自定義 Admin 樣式的實(shí)現(xiàn)

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 代碼

Django 中自定義 Admin 樣式的實(shí)現(xiàn)

看完上述內(nèi)容,你們掌握Django 中自定義 Admin 樣式的實(shí)現(xiàn)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(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)容。

AI