午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

django admin search_fields placeholder 管理后臺添加搜索框提示文字

瀏覽:18日期:2024-09-11 14:40:13

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

django admin search_fields placeholder 管理后臺添加搜索框提示文字

如圖, Django admin后臺生成的搜索框, 默認(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/templatetagstouch carousel/templatetags/__init__.pytouch carousel/templatetags/search_with_placeholder.py

# carousel/templatetags/search_with_placeholder.pyfrom 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/adminmkdir -p carousel/templates/custom_admintouch carousel/templates/admin/search_form_plus.htmltouch 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 method='get'><div><!-- DIV needed for valid HTML --><label for='searchbar'><img src='http://www.leifengta.com.cn/bcjs/{% static 'admin/img/search.svg' %}' alt='Search'></label><input type='text' size='40' name='{{ search_var }}' placeholder='{{ search_placeholder }}' value='{{ cl.query }}' 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='http://www.leifengta.com.cn/bcjs/?{% 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.1from 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 管理后臺添加搜索框提示文字的文章就介紹到這了,更多相關(guān)django admin search_fields placeholder搜索框內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 日本免费一区视频 | 美日韩免费视频 | 粉嫩av网站| 粉嫩欧美一区二区三区 | 中文在线字幕观看 | 欧美日一区二区三区 | 日本女人裸体视频 | 男人的天堂a在线 | 亚洲永久精品在线观看 | 欧美日韩一二三 | 国产又粗又大又硬 | 成年人晚上看的视频 | 天天干狠狠 | 欧美资源网| 一区二区三区四区亚洲 | 丁香婷婷激情五月 | 热久久国产精品 | 丁香色婷婷 | 成人亚洲国产 | 日韩精品在线观看视频 | 成人在线激情 | 国产美女免费观看 | 天美传媒免费在线观看 | www.四虎在线 | 成人毛片a | 国产福利第一页 | 国产一区不卡 | 夜夜春很很躁夜夜躁 | 国产三级短视频 | 亚洲欧美日韩中文在线 | 亚洲图片色图 | 成人激情av | 人人草人人 | av在线色| 村上里沙av | 精品国产福利 | 欧美激情免费视频 | 免费在线观看黄 | 亚洲成年人在线观看 | 五月婷婷综合网 | 粉色午夜视频 |