国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

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

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

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

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

如圖, Django admin后臺生成的搜索框, 默認是沒有提示文字的, 不夠友好; 網上也沒搜到什么好的示例, 于是自己動手實現了一個

0. 已經存在的app名為carousel, 大致相當于如下操作/代碼

$ 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(’小區ID’) class Meta: verbose_name = verbose_name_plural = ’輪播設置’```

1. 定制模板標簽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://m.cgvv.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://m.cgvv.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 = '請輸入小區ID'

其他列表頁, 如果也想顯示提示文字, 只需繼承PlaceholderMixin, 然后定義search_placeholder就可以了

到此這篇關于django admin search_fields placeholder 管理后臺添加搜索框提示文字的文章就介紹到這了,更多相關django admin search_fields placeholder搜索框內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Django
相關文章:
主站蜘蛛池模板: 18女人毛片大全 | 怡红院视频在线观看 | 一区二区三区在线观看视频 | jiucao在线观看精品 | 女人张开腿等男人桶免费视频 | 日本黄大片影院一区二区 | 国产在线手机视频 | 精品三级视频 | 在线观看成年人免费视频 | 日本在线视频不卡 | 欧美一级特黄特色大片免费 | 成人首页 | 女人张开腿男人捅 | 激情午夜天 | 亚洲第一免费视频 | 久久狠狠一本精品综合网 | 亚洲午夜一区二区三区 | 很黄很色的免费视频 | 美女和男人免费网站视频 | 国产99视频精品一区 | 欧美a大片欧美片 | 成年日韩片av在线网站 | 色综合久久88色综合天天提莫 | 真实国产乱子伦高清 | 日本免费一区视频 | 高清日本在线成人免费视频 | 亚洲精品色一区二区三区 | 老司机午夜在线视频免费观 | 午夜看毛片 | 美女一级片视频 | 免费一级真人毛片 | 久久这里有精品视频 | 国模偷拍在线观看免费视频 | 国产农村一二三区 | 国产一级a毛片高清 | 九九在线偷拍视频在线播放 | 免费在线成人 | 免费一级a毛片在线 | 99爱免费观看视频在线 | 欧美大屁股精品毛片视频 | 日本欧美在线视频 |