django admin search_fields placeholder 管理后臺(tái)添加搜索框提示文字
本文主要介紹了django admin search_fields placeholder 管理后臺(tái)添加搜索框提示文字,分享給大家,具體如下:

如圖, Django admin后臺(tái)生成的搜索框, 默認(rèn)是沒(méi)有提示文字的, 不夠友好; 網(wǎng)上也沒(méi)搜到什么好的示例, 于是自己動(dòng)手實(shí)現(xiàn)了一個(gè)
0. 已經(jīng)存在的app名為carousel, 大致相當(dāng)于如下操作/代碼
$ python manage.py startapp carousel
# settings.py
```
INSTALLED_APPS = [
...
'carousel',
]
```
# carousel/models.py
```
from django.db import models
class Carousel(models.Model):
community = models.IntegerField('小區(qū)ID')
class Meta:
verbose_name = verbose_name_plural = '輪播設(shè)置'
```
1. 定制模板標(biāo)簽templatetags
mkdir -p carousel/templatetags touch carousel/templatetags/__init__.py touch carousel/templatetags/search_with_placeholder.py
# carousel/templatetags/search_with_placeholder.py
from django.contrib.admin.templatetags.admin_list import (
InclusionAdminNode,
register,
search_form,
)
def search_form_plus(cl, search_placeholder: str = ""):
"""
Display a search form for searching the list with placeholder.
"""
return dict(search_form(cl), search_placeholder=search_placeholder)
@register.tag(name="search_form_plus")
def search_form_tag(parser, token):
return InclusionAdminNode(
parser,
token,
func=search_form_plus,
template_name="search_form_plus.html",
takes_context=False,
)
2. 定制模板template
mkdir -p carousel/templates/admin mkdir -p carousel/templates/custom_admin touch carousel/templates/admin/search_form_plus.html touch carousel/templates/custom_admin/change_list.html
<!-- carousel/templates/admin/search_form_plus.html -->
{% load i18n static %}
{% if cl.search_fields %}
<div id="toolbar"><form id="changelist-search" method="get">
<div><!-- DIV needed for valid HTML -->
<label for="searchbar"><img src="{% static "admin/img/search.svg" %}" alt="Search"></label>
<input type="text" size="40" name="{{ search_var }}" placeholder="{{ search_placeholder }}" value="{{ cl.query }}" id="searchbar" autofocus>
<input type="submit" value="{% translate 'Search' %}">
{% if show_result_count %}
<span class="small quiet">{% blocktranslate count counter=cl.result_count %}{{ counter }} result{% plural %}{{ counter }} results{% endblocktranslate %} (<a href="?{% if cl.is_popup %}_popup=1{% endif %}" rel="external nofollow" >{% if cl.show_full_result_count %}{% blocktranslate with full_result_count=cl.full_result_count %}{{ full_result_count }} total{% endblocktranslate %}{% else %}{% translate "Show all" %}{% endif %}</a>)</span>
{% endif %}
{% for pair in cl.params.items %}
{% if pair.0 != search_var %}<input type="hidden" name="{{ pair.0 }}" value="{{ pair.1 }}">{% endif %}
{% endfor %}
</div>
</form></div>
{% endif %}
<!-- carousel/templates/custom_admin/change_list.html -->
{% extends "admin/change_list.html" %}
{% load search_with_placeholder %}
{% block search %}{% search_form_plus cl search_placeholder %}{% endblock %}
3. 定制admin.py
cat carousel/admin.py
# Django3.1
from django.contrib import admin
from .models import BoxCarousel, Carousel,
class PlaceholderMixin:
change_list_template = "custom_admin/change_list.html"
def changelist_view(self, request, extra_context=None):
search_placeholder = getattr(self, "search_placeholder", False)
if search_placeholder:
extra_context = extra_context or {}
extra_context["search_placeholder"] = search_placeholder
return super().changelist_view(request, extra_context)
@admin.register(Carousel)
class CarouselAdmin(PlaceholderMixin, admin.ModelAdmin):
search_fields = ["=community"]
search_placeholder = "請(qǐng)輸入小區(qū)ID"
其他列表頁(yè), 如果也想顯示提示文字, 只需繼承PlaceholderMixin, 然后定義search_placeholder就可以了
到此這篇關(guān)于django admin search_fields placeholder 管理后臺(tái)添加搜索框提示文字的文章就介紹到這了,更多相關(guān)django admin search_fields placeholder搜索框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Win10?Anaconda?新建環(huán)境安裝python-pcl的步驟
這篇文章主要介紹了Win10?Anaconda?新建環(huán)境安裝python-pcl的方法,至于VS環(huán)境下安裝C++?版本的pcl也可以按照此文提供的步驟安裝實(shí)現(xiàn),需要的朋友可以參考下2022-04-04
Python多個(gè)MP4合成視頻的實(shí)現(xiàn)方法
最近接觸了個(gè)項(xiàng)目,需要把多個(gè)文件合成一個(gè)視頻,本文主要使用Python把多個(gè)MP4合成視頻,感興趣的可以了解一下2021-07-07
Python實(shí)現(xiàn)隨機(jī)生成算術(shù)題的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)隨機(jī)生成算術(shù)題的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-04-04
Python機(jī)器學(xué)習(xí)算法之決策樹(shù)算法的實(shí)現(xiàn)與優(yōu)缺點(diǎn)
決策樹(shù)(Decision Tree)是一種基本的分類與回歸方法,這篇文章主要給大家介紹了關(guān)于Python機(jī)器學(xué)習(xí)算法之決策樹(shù)算法實(shí)現(xiàn)與優(yōu)缺點(diǎn)的相關(guān)資料,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Flask中基于Token的身份認(rèn)證的實(shí)現(xiàn)
本文主要介紹了Flask中基于Token的身份認(rèn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python如何根據(jù)字幕文件自動(dòng)給視頻添加字幕效果
視頻中字幕的重要性不用多說(shuō)了,下面這篇文章主要給大家介紹了關(guān)于Python如何根據(jù)字幕文件自動(dòng)給視頻添加字幕效果的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02

