Add help_text for search field in admin.py

2019-02-17 10:08发布

How can I add help_text in django for a search field I am using in admin.py as:

class ProfileAdmin(admin.ModelAdmin):

    list_display = ('First_Name','Last_Name','Registeration_No','University','Batch','Sex')

    search_fields = ('First_Name','Last_Name','Registeration_No','University','Batch')

4条回答
唯我独甜
2楼-- · 2019-02-17 10:41

You can add this javascript in ModelAdmin

window.onload = function() {
    document.getElementById("searchbar").placeholder = "search with ";
};

ModelAdmin will be like

class SomeModelAdmin(admin.ModelAdmin):

    class Media:
        js = ('js/admin/custom_admin.js',)
        # css = { 'all': ('css/admin/custom_admin.css',)}

add the custom_admin.js in static/js/admin directory.

查看更多
等我变得足够好
3楼-- · 2019-02-17 10:49

Answer just for myself!

  1. Add an attr named search_fields_hint to my ModelAdmin subclass.
  2. Edit admin.templates.admin.search_form.html
  3. Add a attr placeholder="{{ cl.search_fields_hint }} to the <input>.
  4. Edit admin.views.main.py, ChangeList class
  5. Add self.search_fields_hint=search_fields_hint to the __init__.
  6. Edit admin.options.py, about line 1468, add parameter self.search_fields_hint to ChangList.
查看更多
贪生不怕死
4楼-- · 2019-02-17 10:52

The easiest solution is with jquery:

$("#searchfield_id").attr('title', "Click here to search for someone")

Or you can add it directly to the HTML itself. The title shows up when you mouse over the search field.

查看更多
你好瞎i
5楼-- · 2019-02-17 10:58

You could either override Admin template admin/search_form.html to add help text;
Or load a javascript file, which could find the dom node to insert the help text, in ProfileAdmin.Media, check the doc.

查看更多
登录 后发表回答