Ticket #3698: newforms_labling.2.patch

File newforms_labling.2.patch, 2.7 KB (added by Chris Beaven, 17 years ago)

with tests

  • django/newforms/forms.py

     
    121121            else:
    122122                if errors_on_separate_row and bf_errors:
    123123                    output.append(error_row % bf_errors)
    124                 label = bf.label and bf.label_tag(escape(bf.label + ':')) or ''
     124                if bf.label:
     125                    label = escape(bf.label)
     126                    if label[-1] not in ':?.!':
     127                        # Only add a colon if the label does not end in punctuation.
     128                        label += ':'
     129                    label = bf.label_tag(label) or ''
     130                else:
     131                    label = ''
    125132                if field.help_text:
    126133                    help_text = help_text_html % field.help_text
    127134                else:
  • tests/regressiontests/forms/tests.py

     
    25712571<li>Password1: <input type="password" name="password1" /></li>
    25722572<li>Password (again): <input type="password" name="password2" /></li>
    25732573
     2574Labels for as_* methods should only end in a colon if they don't end in other
     2575punctuation already.
     2576>>> class Questions(Form):
     2577...    q1 = CharField(label='The first question')
     2578...    q2 = CharField(label='What is your name?')
     2579...    q3 = CharField(label='The answer to life is:')
     2580...    q4 = CharField(label='Answer this question!')
     2581...    q5 = CharField(label='The last question. Period.')
     2582>>> print Questions(auto_id=False).as_p()
     2583<p>The first question: <input type="text" name="q1" /></p>
     2584<p>What is your name? <input type="text" name="q2" /></p>
     2585<p>The answer to life is: <input type="text" name="q3" /></p>
     2586<p>Answer this question! <input type="text" name="q4" /></p>
     2587<p>The last question. Period. <input type="text" name="q5" /></p>
     2588>>> print Questions().as_p()
     2589<p><label for="id_q1">The first question:</label> <input type="text" name="q1" id="id_q1" /></p>
     2590<p><label for="id_q2">What is your name?</label> <input type="text" name="q2" id="id_q2" /></p>
     2591<p><label for="id_q3">The answer to life is:</label> <input type="text" name="q3" id="id_q3" /></p>
     2592<p><label for="id_q4">Answer this question!</label> <input type="text" name="q4" id="id_q4" /></p>
     2593<p><label for="id_q5">The last question. Period.</label> <input type="text" name="q5" id="id_q5" /></p>
     2594
    25742595A label can be a Unicode object or a bytestring with special characters.
    25752596>>> class UserRegistration(Form):
    25762597...    username = CharField(max_length=10, label='ŠĐĆŽćžšđ')
Back to Top