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

作者:waketzheng 时间:2022-02-11 14:31:53 

本文主要介绍了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/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 = "请输入小区ID"

其他列表页, 如果也想显示提示文字, 只需继承PlaceholderMixin, 然后定义search_placeholder就可以了

来源:https://blog.csdn.net/jaket5219999/article/details/115029520

标签:django,admin,search,fields,placeholder,搜索框
0
投稿

猜你喜欢

  • asp.net连接查询SQL数据库并把结果显示在网页上(2种方法)

    2024-01-12 13:28:10
  • perl 采集入库脚本分享

    2023-09-13 08:45:40
  • 微信小程序实战之打卡时钟的绘制

    2024-04-17 10:35:32
  • PDO::errorCode讲解

    2023-06-08 03:39:17
  • Python多个MP4合成视频的实现方法

    2021-02-21 13:50:44
  • Fiddler如何抓取手机APP数据包

    2023-12-02 04:18:57
  • 解决mybatis使用char类型字段查询oracle数据库时结果返回null问题

    2024-01-26 03:05:05
  • 如何利用Python随机从list中挑选一个元素

    2023-08-04 00:05:54
  • 怎样安全地关闭MySQL实例

    2024-01-20 02:39:20
  • python指定路径斜杠与反斜杠遇到的问题

    2023-03-18 20:47:27
  • 来自qq的javascript面试题

    2024-04-16 10:29:17
  • python+JS 实现逆向 SMZDM 的登录加密

    2023-09-22 05:38:55
  • win2003安装sqlserver 2000提示无法验证产品密钥的解决方法

    2024-01-27 00:18:39
  • python 两个数据库postgresql对比

    2024-01-22 16:29:36
  • python实现快递价格查询系统

    2023-02-06 11:20:27
  • pythotn条件分支与循环详解(3)

    2023-11-14 01:35:06
  • 不错的广告定位效果代码

    2009-06-05 18:51:00
  • Python实现字典按照value进行排序的方法分析

    2022-11-03 04:27:27
  • 无序列表 li ul

    2008-07-29 13:00:00
  • Python 实现大整数乘法算法的示例代码

    2022-07-07 02:57:54
  • asp之家 网络编程 m.aspxhome.com