背景
最近做項(xiàng)目開發(fā)出現(xiàn)一個(gè)需求,就是前端會(huì)發(fā)來(lái)用戶對(duì)某一項(xiàng)內(nèi)容的報(bào)錯(cuò),報(bào)錯(cuò)信息中包含出錯(cuò)內(nèi)容的id,為了方便管理,需要實(shí)現(xiàn)點(diǎn)擊這個(gè)id直接轉(zhuǎn)達(dá)相應(yīng)內(nèi)容的詳情頁(yè)面。
效果展示
解決
首先在django admin的列表中查看數(shù)據(jù)id所代表的鏈接
使用瀏覽器的檢查元素功能查看點(diǎn)擊該id所跳轉(zhuǎn)的鏈接
現(xiàn)在我們知道點(diǎn)擊admin頁(yè)面跳轉(zhuǎn)鏈接的格式了。
自定義widget
假如我們的內(nèi)容id使用CharField字段存儲(chǔ),那么可以這樣自定義一個(gè)widget
HTML
{% load myfileter %}
{{ widget.value }}
{%load myfilter%}是引入自定義模板標(biāo)簽的語(yǔ)句,該標(biāo)簽在下面定義。
python
class MyWidget(TextInput): template_name = "myWidget.html" def render(self, name, value, attrs=None, renderer=None): context = self.get_context(name, value, attrs) template = loader.get_template(self.template_name).render(context) return mark_safe(template)