django-crispy-forms : help_text_inline of FormHelp

2019-08-03 17:55发布

问题:

For the following model:

class MyModel(models.Model):
    name = models.CharField(max_length=110, help_text="Some sample help text.")
    def __unicode__(self):
        return u'%s' % (self.name)

And the following modelform:

class MyModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyModelForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-myModelForm'
        self.helper.form_class = 'form-horizontal'
        self.helper.form_action = 'my_model_form_url'
        self.helper.form_error_title = 'Form Errors'
        self.helper.help_text_inline = True
    class Meta:
        model = MyModel

Using the following template:

{% extends "base.html" %}

{% block content %}
    {% load crispy_forms_tags %}
    {% crispy form %}
{% endblock %}

The help_text defined in the model does not get rendered at all. It does get rendered if I change to self.helper.help_text_inline = False instead of self.helper.help_text_inline = True, but that's not what I want.

How do I get the help_text to show with self.helper.help_text_inline = True?

The base.html is all proper with bootstrap files all included.

回答1:

This is due to the fact that help_text_inline and error_text_inline cannot be set to the same value in order to work. If you set help_text_inline to True, you need to set error_text_inline to False. If you don't do it, help text messages are not displayed, in order to show form errors in case they happen.

I hadn't thought about this in detail until now. So probably the best would be to add a logging warning, telling the user to be careful with this. Maybe automatically overriding the default behavior of the other flag, in case one is set. I'm open to suggestions.