user26
user26

Reputation: 4327

Can i add help text in django model fields

I have a Student detail page where I have all the student data shown in a log nested format.

Now in the form I know I can add a help text. But now my manager wants that when we show the detail page, there should be help on hovering over each field.

Now I am confused where should I enter 50-100 words help text for each table in 5-6 tables

This is how I used help_text in the forms:

student_number = forms.CharField(
        required=False, max_length=64, label='Student Number',
        help_text='Unique identifier for the student ')

Upvotes: 34

Views: 50784

Answers (5)

Radren
Radren

Reputation: 305

After add help_texts in form, you should do smth like this in a frontend:

<label title="{{ form.name.help_text }}" for="{{ form.name.id_for_label }}">Your label</label> 
{{ form.name }} {{ form.quantity }}

Upvotes: 6

tomcounsell
tomcounsell

Reputation: 5081

When using model forms you can add labels and help_texts to fields generated by the model. see docs

class PupilForm(forms.ModelForm):

  class Meta:
    model = Pupil

    fields = ['student_number',]
    labels = {'student_number': "Student Number",}
    help_texts = {'student_number': "Unique identifier for the student",}

Upvotes: 9

ecp
ecp

Reputation: 2419

In case you want to use the standard admin change form with a short help-text for each field but sometimes feel the need to give a longer explanation or even a table with some sample values (without restricting the user to a predefined set of choices) you can do something like this:

my_strange_field = models.FloatField('strange parameter', validators=[MinValueValidator(1.0)],
                                     help_text='corr. factor x >= 1 <img src="/static/admin/img/icon-unknown.gif" '
                                               'width="15" height="15" title="typical values:\n'
                                               'cow:\t2..3\ncat:\t5..7\ndog:\t11..15')

This way you get a short text "corr. factor x >= 1" followed by a nifty question mark which presents a table like tool-tip without the need to modify the change_form template.

Upvotes: 2

nim4n
nim4n

Reputation: 1829

Yes you can! Just like your form, you can add help_text to your model fields.

Upvotes: 59

djangonaut
djangonaut

Reputation: 7778

By 'detail page' you mean a edit form of a single student instance or the list of all student records? Are you using Django admin or do you use your own view and template, custom form definition or as_ul()/as_list() etc? It's hard to answer your question with just seeing your form field definition.

What do you mean by 'for each table'? Would form inheritance help, so that you set the help text of common form fields only in the super form.

If you render a custom template you can render the help_text wherever you like with {{ my_field.help_text }}. If you have a table-like view in your template and want the helptext there, just put an empty instance of the form in your template context so that you have access to the help_texts and put it in your table as tooltip?

Upvotes: 4

Related Questions