欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django admin search_fields placeholder 管理后臺(tái)添加搜索框提示文字

 更新時(shí)間:2021年03月23日 10:02:10   作者:waketzheng  
這篇文章主要介紹了django admin search_fields placeholder 管理后臺(tái)添加搜索框提示文字,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

如圖, Django admin后臺(tái)生成的搜索框, 默認(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/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 = "請輸入小區(qū)ID"

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

到此這篇關(guān)于django admin search_fields placeholder 管理后臺(tái)添加搜索框提示文字的文章就介紹到這了,更多相關(guān)django admin search_fields placeholder搜索框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python實(shí)現(xiàn)排序算法、查找算法和圖遍歷算法實(shí)例

    Python實(shí)現(xiàn)排序算法、查找算法和圖遍歷算法實(shí)例

    這篇文章主要介紹了Python實(shí)現(xiàn)排序算法、查找算法和圖遍歷算法實(shí)例,排序算法、查找算法和圖遍歷算法是計(jì)算機(jī)科學(xué)中常見且重要的算法。它們在數(shù)據(jù)處理、搜索和圖結(jié)構(gòu)等領(lǐng)域發(fā)揮著關(guān)鍵作用,需要的朋友可以參考下
    2023-08-08
  • python format 格式化輸出方法

    python format 格式化輸出方法

    今天小編就為大家分享一篇python format 格式化輸出方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 利用python將圖片轉(zhuǎn)換成excel文檔格式

    利用python將圖片轉(zhuǎn)換成excel文檔格式

    編寫了一小段Python代碼,將圖片轉(zhuǎn)為了Excel,純屬娛樂,下面這篇文章主要給大家介紹了關(guān)于利用python將圖片轉(zhuǎn)換成excel文檔格式的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • Python中的函數(shù)參數(shù)類型檢查

    Python中的函數(shù)參數(shù)類型檢查

    這篇文章主要介紹了Python中的函數(shù)參數(shù)類型檢查,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • python爬蟲之驗(yàn)證碼篇3-滑動(dòng)驗(yàn)證碼識(shí)別技術(shù)

    python爬蟲之驗(yàn)證碼篇3-滑動(dòng)驗(yàn)證碼識(shí)別技術(shù)

    本篇涉及到的驗(yàn)證碼為滑動(dòng)驗(yàn)證碼,不同于極驗(yàn)證,本驗(yàn)證碼難度略低,需要的將滑塊拖動(dòng)到矩形區(qū)域右側(cè)即可完成。對python爬蟲滑動(dòng)驗(yàn)證碼識(shí)別技術(shù)感興趣的朋友跟隨小編一起看看吧
    2019-04-04
  • python錄音并調(diào)用百度語音識(shí)別接口的示例

    python錄音并調(diào)用百度語音識(shí)別接口的示例

    這篇文章主要介紹了python錄音并調(diào)用百度語音識(shí)別接口的示例,幫助大家更好的理解和利用python處理音頻,感興趣的朋友可以了解下
    2020-12-12
  • Django csrf校驗(yàn)的實(shí)現(xiàn)

    Django csrf校驗(yàn)的實(shí)現(xiàn)

    這篇文章主要介紹了Django csrf校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • python Tornado事件循環(huán)示例源碼解析

    python Tornado事件循環(huán)示例源碼解析

    這篇文章主要為大家介紹了python Tornado事件循環(huán)示例源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • python 將字符串中的數(shù)字相加求和的實(shí)現(xiàn)

    python 將字符串中的數(shù)字相加求和的實(shí)現(xiàn)

    這篇文章主要介紹了python 將字符串中的數(shù)字相加求和的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • pygame游戲之旅 載入小車圖片、更新窗口

    pygame游戲之旅 載入小車圖片、更新窗口

    這篇文章主要為大家詳細(xì)介紹了pygame游戲之旅的第3篇,教大家如何載入小車圖片、更新窗口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11

最新評論