django admin search_fields placeholder 管理后臺(tái)添加搜索框提示文字
本文主要介紹了django admin search_fields placeholder 管理后臺(tái)添加搜索框提示文字,分享給大家,具體如下:
如圖, Django admin后臺(tái)生成的搜索框, 默認(rèn)是沒有提示文字的, 不夠友好; 網(wǎng)上也沒搜到什么好的示例, 于是自己動(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ū)ID"
其他列表頁, 如果也想顯示提示文字, 只需繼承PlaceholderMixin, 然后定義search_placeholder就可以了
到此這篇關(guān)于django admin search_fields placeholder 管理后臺(tái)添加搜索框提示文字的文章就介紹到這了,更多相關(guān)django admin search_fields placeholder搜索框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)排序算法、查找算法和圖遍歷算法實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)排序算法、查找算法和圖遍歷算法實(shí)例,排序算法、查找算法和圖遍歷算法是計(jì)算機(jī)科學(xué)中常見且重要的算法。它們在數(shù)據(jù)處理、搜索和圖結(jié)構(gòu)等領(lǐng)域發(fā)揮著關(guān)鍵作用,需要的朋友可以參考下2023-08-08利用python將圖片轉(zhuǎn)換成excel文檔格式
編寫了一小段Python代碼,將圖片轉(zhuǎn)為了Excel,純屬娛樂,下面這篇文章主要給大家介紹了關(guān)于利用python將圖片轉(zhuǎn)換成excel文檔格式的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-12-12python爬蟲之驗(yàn)證碼篇3-滑動(dòng)驗(yàn)證碼識(shí)別技術(shù)
本篇涉及到的驗(yàn)證碼為滑動(dòng)驗(yàn)證碼,不同于極驗(yàn)證,本驗(yàn)證碼難度略低,需要的將滑塊拖動(dòng)到矩形區(qū)域右側(cè)即可完成。對python爬蟲滑動(dòng)驗(yàn)證碼識(shí)別技術(shù)感興趣的朋友跟隨小編一起看看吧2019-04-04python錄音并調(diào)用百度語音識(shí)別接口的示例
這篇文章主要介紹了python錄音并調(diào)用百度語音識(shí)別接口的示例,幫助大家更好的理解和利用python處理音頻,感興趣的朋友可以了解下2020-12-12Django csrf校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了Django csrf校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05python Tornado事件循環(huán)示例源碼解析
這篇文章主要為大家介紹了python Tornado事件循環(huán)示例源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09python 將字符串中的數(shù)字相加求和的實(shí)現(xiàn)
這篇文章主要介紹了python 將字符串中的數(shù)字相加求和的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07