Form field description in django admin

47,201

Solution 1

When defining your fields in models.py:

myfield = models.CharField(max_length=100, help_text="This is the grey text")

Bookmark this link:

https://docs.djangoproject.com/en/dev/ref/models/fields/#help-text

I find myself referring to it all the time (not just for help_text, but for everything to do with model fields)!

Solution 2

In addition to Timmy's answer, if you want to display some additional help text and want to have some helpful markup too, you can custom display individual form fieldsets and add a description field. Using your example, let's say that you wanted to break out the Content field into it's own fieldset block and add some verbose help text. You can do something like:

from mymodel.models import MyModel
from django.contrib import admin

"""
Custom Help Text
"""
CONTENT_HELP_TEXT = ' '.join(['<p>Here is some multi-line help',
                              'which is a long string so put',
                              'into a list which is then joined',
                              'with spaces. I can do fun things',
                              'like have <strong>bold</strong>',
                              'and some line breaks.<br/>'])
"""
Customize Admin
"""
class MyModelAdmin(admin.ModelAdmin):
    """
    Add your other customizations
    like actions, list_display, list filter, etc
    """
    fieldsets = [
        ('Content', {
            'fields':('content',),
            'description': '<div class="help">%s</div>' % CONTENT_HELP_TEXT,
        }),
    ]

admin.site.register(MyModel, MyModelAdmin)

More information in the Django docs (scroll down to the fieldsets) area.

Solution 3

In your forms.py file, after the

fields = ['URL',....]

add

help_texts = {"URL": "Example..."}
Share:
47,201

Related videos on Youtube

megido
Author by

megido

Updated on December 27, 2020

Comments

  • megido
    megido over 3 years

    How to add hint for the form field in django admin like in next example?

    form field description in django admin

    (here: URL and Content descriptions are shown with gray color under field)

  • Mechanical snail
    Mechanical snail almost 13 years
    I think you've got the wrong link, since we're talking about models.FooField rather than forms.FooField. It should be docs.djangoproject.com/en/dev/ref/models/fields/#help-text
  • pranavk
    pranavk over 11 years
    and what about adding a multiline field like description ?
  • thevasya
    thevasya over 10 years
    there should be a comma after 'content' inside parentheses
  • radtek
    radtek over 9 years
    The accepted answer is the standard for forms, but this is the best answer as its independent of the forms and relies on the fieldset breakdown as set by the model admin. Thanks!
  • RickyA
    RickyA almost 8 years
    @pranavk it is html so you can use <br/> for line breaks
  • ccpizza
    ccpizza almost 6 years
    no need to join string lists with spaces — you can simply use backward slash characters `\` to continue the string on the next line without inserting a line break: docs.python.org/3.2/tutorial/introduction.html#strings
  • tatlar
    tatlar almost 5 years
    @diegueus9: Your edit to my answer has introduced a grammatical error: you have removed the white space between the words per new line which garbles the sentence. Please fix or roll back to the original.
  • tatlar
    tatlar almost 5 years
    @diegueus9: I rolled back to a previous version of the answer. Please double-check your proposed changes for syntax before reinstating.