應(yīng)用下adminx.py 部分代碼
....
from django.utils.safestring import mark_safe
# 自定義操作按鈕
def buttons(self, obj):
button_html = "<a class='changelink' href='%s/update/''>編輯</a>"%obj.id + " <a class='changelink' href='%s/delete/''>刪除</a>"%obj.id
return mark_safe(button_html)
buttons.short_description = "操作"
# 添加到list_display
list_display = ['id','title','category','modified_time','views','status','over_time','buttons']
...
models.py部分代碼
...
class Post(models.Model):
"""文章"""
status_choices = (
(0, '未通過'),
(1, '已通過'),
(2, '草稿')
)
title = models.CharField(max_length=100,verbose_name=u'標(biāo)題')
body = UEditorField(verbose_name=u'內(nèi)容',width=600, height=300,toolbars="full",imagePath="advices/ueditor/%(datetime)s.%(extname)s",filePath="advices/ueditor/%(datetime)s.%(extname)s", default='')
created_time = models.DateTimeField(verbose_name=u'錄入時(shí)間',auto_now_add=True) #不出現(xiàn)在后臺(tái)
modified_time = models.DateTimeField(verbose_name=u'更新時(shí)間',default = timezone.now) #不出現(xiàn)在后臺(tái)
modified_time.editable = True #出現(xiàn)在后臺(tái)
excerpt = models.CharField(max_length=200, blank=True,verbose_name=u'簡介') # 文章摘要菲嘴,可為空
category = models.ForeignKey('Category',to_field='id',on_delete='models.CASCADE',verbose_name=u'欄目',default='') # ForeignKey表示1對(duì)多(多個(gè)post對(duì)應(yīng)1個(gè)category)
tags = models.ManyToManyField(Tag, blank=True,verbose_name=u'標(biāo)簽')
views = models.PositiveIntegerField(default=0,verbose_name=u'點(diǎn)擊數(shù)') # 閱讀量
status = models.SmallIntegerField(default=2,choices=status_choices,verbose_name=u'狀態(tài)') # 狀態(tài)
def over_time(self):
if self.created_time:
now = timezone.now()
month = now.month - self.created_time.month
day = now.day - self.created_time.day
if not month:
text = "<p style='color:red'>已發(fā)表{}天<p/>".format(day)
else:
text = "<p style='color:red'>已發(fā)表{}月{}天<p/>".format(month, day)
return mark_safe(text)
else:
return mark_safe("")
over_time.short_description = '發(fā)表時(shí)間'
class Meta:
# 設(shè)置model在后臺(tái)管理系統(tǒng)中的中文名稱
verbose_name = u'博客'
verbose_name_plural = verbose_name
unique_together = ('title', 'category')
def __str__(self):
return self.title
...