Display some free text in between Django Form fields

前端 未结 8 635
无人及你
无人及你 2021-02-04 09:00

I have a form like the following:

class MyForm(Form):

  #personal data
  firstname = CharField()
  lastname = CharField()

  #education data
  university = Char         


        
相关标签:
8条回答
  • 2021-02-04 09:07

    For those into a similar situation as the author, I recommend falling back to CSS and the :before and/or :after pseudoselectors either on the input or the label elements of the form. They work just as well and could make your life a lot easier as you can still use {{ form.as_p }}.

    0 讨论(0)
  • 2021-02-04 09:15

    Since each section is a collection of multiple, independent form fields, I recommend using a custom form template. This gives you absolute full control over the layout with minimal extra work. Django's Customizing the Form Template docs have the details.

    0 讨论(0)
  • 2021-02-04 09:22

    There is a help_text field in forms you know.

    in forms.py:

    • myField = forms.myFieldType(help_text="Helping friendly text to put in your form", otherStuff=otherStuff)

    in forms.html:

    • {{form.myField.help_text}}
    0 讨论(0)
  • 2021-02-04 09:27

    Even though your form is populated from a database, you should still be able to manually write out the form, or use a template. Check out the Django form documentation for more details.

    0 讨论(0)
  • 2021-02-04 09:27

    agreed with @mipadi, seems it's the easiest way.

    So something like

    $('.selector').append('<html></html>')
    
    0 讨论(0)
  • 2021-02-04 09:28

    One way to do this without displaying your form in the template using form.as_ul, is with django-uni-form. First you'll have to download it here and install it. Then the code for setting up your form could looks something like this:

    from uni_form.helpers import FormHelper, Submit, Layout, Fieldset
    
    class MyForm(Form):
    
        #personal data
        firstname = CharField()
        lastname = CharField()
    
        #education data
        university = CharField()
        major = CharField()
    
        #foobar data
        foobar = ChoiceField()
    
        # now attach a uni_form helper to display the form
        helper = FormHelper()
    
        # create the layout
        layout = Layout(
             # first fieldset
             Fieldset("Here you enter your personal data...",
                 'firstname', 'lastname'),
             Fieldset("Here you enter your education data...",
                 'university', 'major'),
             Fieldset('foobar')
    
        # and add a submit button
        sumbit = Submit('add', 'Submit information')
        helper.add_input(submit)
    

    Now, to display this in your template you would do this:

    {% load uni_form %}
    {% with form.helper as helper %}
        {% uni_form form helper %}
    {% endwith %}
    

    This would output HTML (roughly) like this:

    <form>
    <fieldset><legend>Here you enter your personal data...</legend>
    <input name='firstname'>
    <input name='lastname'>
    </fieldset>
    
    <fieldset><legend>Here you enter your education data...</legend>
    <input name='university'>
    <input name='major'>
    </fieldset>
    
    <fieldset>
    <input name='foobar'>
    </fieldset>
    
    </form>
    

    For more info on uni_form, read their docs (see the link above).

    PS: I realize this reply is late, and I'm sure you already solved this problem, but I think this should be helpful for anyone just coming across this now.

    0 讨论(0)
提交回复
热议问题