How to render form field with information that it is required

前端 未结 5 935
孤街浪徒
孤街浪徒 2020-12-06 04:35

Is there any clever way to make django forms render field with asterisks after fields that are required? Or to provide some other clever for to mark required fields? I would

5条回答
  •  不思量自难忘°
    2020-12-06 04:53

    Personny I tried something like this, i.e. overriding the default template tag (this adds a red asterisk in all the admin interface for required not-readonly fields. Then i also defined a 2nd tag for my views that mix inline and block labels. See the code below (copy/paste of Django source code with some modifications):

    In settings.py :

    from django.forms.util import flatatt
    from django.utils.html import conditional_escape
    from django.utils.safestring import mark_safe
    import django.forms.forms as django_forms
    def custom_label_tag(self, contents=None, attrs=None):
            """
            Wraps the given contents in a 

    In my templates

    {{ form.fieldname.errors}}{{ form.fieldname.inline_label_tag }}{{form.fieldname}}

    OR

    {{ form.fieldname.errors}}{{ form.fieldname.label_tag }}{{form.fieldname}}

提交回复
热议问题