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


猜你喜欢
- 本文为大家分享了SQLServer存储过程中事务的使用方法,具体代码如下create proc usp_Stock@GoodsId int,
- 一、前言进程,一个新鲜的字眼,可能有些人并不了解,它是系统某个运行程序的载体,这个程序可以有单个或者多个进程,一般来说,进程是通过系统CPU
- python简单游戏-反弹球,供大家参考,具体内容如下tkinter实现,直接贴上代码from tkinter import*import
- CSS写法:overflow-y:visible本代码适用平台:IE6,IE7演示:<textarea style="wid
- 之前摸索tensorflow的时候安装踩坑的时间非常久,主要是没搞懂几个东西的关系,就在瞎调试,以及当时很多东西不懂,很多报错也一知半解的。
- 一、文件形式的邮件#!/usr/bin/env python3#coding: utf-8import smtplibfrom email.
- 没办法就下了一个2.6,如果用2.4就太低了,又折腾了,半天找到了MySQL-python-1.2.2.win32-py2.6.exe 这个
- 列名用了中文的缘故,设置pandas的参数即可,代码如下: import pandas as pd #这两个参数的默认设置都是False p
- 最近拾回Django学习,实例练习中遇到了对多维字典类型数据的遍历操作问题,Google查询没有相关资料…毕竟是新手,到自己动手时发现并非想
- 一、功能介绍1.MySQL Servers该功能是mysql主要的服务,也是必须安装的功能。2.Mysql WorkBench这个是mysq
- <!DOCTYPE html> <html> <head> <meta charset="
- 日常工作中需要对比两个Excel工作表中的数据差异是很不方便的,使用python来做就比较简单了!我们的思路是通过读取两个Excel的数据,
- 增加字段alter table docdsp add dspcode char(200)删除字段ALTER TABLE tabl
- 本文实例分析了JS限制条件补全问题。分享给大家供大家参考,具体如下:题目一.a和b两个变量,不用第三个变量来切换两个变量值var a=5;v
- var yData = [];//Y轴数据 var xData = [];//X轴数据 $(data.rows).each(function
- 在go语言中,byte其实是uint8的别名,byte 和 uint8 之间可以直接进行互转。目前来只能将0~255范围的int转成byte
- 以下为SQL SERVER7.0以上版本的字段类型说明。SQL SERVER6.5的字段类型说明请参考SQL SERVER提供的说明。bit
- php屏蔽电话号码中间四位:Method 1:function hidtel($phone){
- 本文将介绍如何基于Python Django实现验证码登录功能。验证码登录是一种常见的身份验证方式,它可以有效防止恶意攻击和机器人登录。本文
- numpy数组的广播功能强大,但是也同时让人疑惑不解,现在让我们来谈谈其中的原理。广播原则:如果两个数组的后缘维度(即:从末尾开始算起的维度