Editing Django _form.as_p

前端 未结 5 1361
名媛妹妹
名媛妹妹 2021-02-02 11:38

By default _form.as._p spits out:

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-02 12:11

    Pretty much what Brian describes above. I would write a new method for your form like as_myp. I made this for myself. I took as_table method and made as_plain to remove the tr/th markups. Ex.

    class MyForm(forms.Form):
        my_field1 = forms.CharField(...)
        my_field2 = forms.WhateverField(...)
    
        def as_myp(self):
            "Returns this form rendered as HTML 

    s." return self._html_output( normal_row = '%(label)s

    %(field)s%(help_text)s

    ', error_row = '%s', row_ender = '

    ', help_text_html = ' %s', errors_on_separate_row = True) def as_plain(self): "Returns this form rendered as HTML s -- excluding the
    ." return self._html_output( normal_row = '%(label)s%(errors)s%(field)s%(help_text)s', error_row = '%s', row_ender = ' ', help_text_html = '
    %s', errors_on_separate_row = False)

    It just seemed easier to do that than write a template file and handle form field rendering with errors, tags, visible/hidden, etc.

提交回复
热议问题