gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章django admin search_fields placeholder 管理后台添加搜索框提示文字由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文主要介绍了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 id="changelist-search" method="get"><div><!-- DIV needed for valid HTML --><label for="searchbar"><img src="{% static "admin/img/search.svg" %}" 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.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搜索框内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://blog.csdn.net/jaket5219999/article/details/115029520 。

最后此篇关于django admin search_fields placeholder 管理后台添加搜索框提示文字的文章就讲到这里了,如果你想了解更多关于django admin search_fields placeholder 管理后台添加搜索框提示文字的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com