Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form field description in django admin

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)

like image 255
megido Avatar asked Jun 03 '11 18:06

megido


People also ask

How do I get form fields in Django?

Basically to extract data from a form field of a form, all you have to do is use the form. is_valid() function along with the form. cleaned_data. get() function, passing in the name of the form field into this function as a parameter.

What is form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

How do you define choice fields in Django?

Choices can be any sequence object – not necessarily a list or tuple. The first element in each tuple is the actual value to be set on the model, and the second element is the human-readable name. Let us create a choices field with above semester in our django project named geeksforgeeks.


3 Answers

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)!

like image 190
Timmy O'Mahony Avatar answered Oct 27 '22 10:10

Timmy O'Mahony


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.

like image 31
tatlar Avatar answered Oct 27 '22 09:10

tatlar


In your forms.py file, after the

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

add

help_texts = {"URL": "Example..."}
like image 1
Shishir Avatar answered Oct 27 '22 09:10

Shishir