Ticket #4232: 4232.2.diff

File 4232.2.diff, 4.2 KB (added by heckj@…, 17 years ago)

Update to the diff after checkins [5219], [5222], and [5237] (easier merge)

  • docs/newforms.txt

     
    313313    ...
    314314    AttributeError: 'ContactForm' object has no attribute 'cleaned_data'
    315315
     316
     317Example View
     318~~~~~~~~~~~~
     319
     320Putting this all together, here is a simple view method for the example of our contact form:
     321
     322    from django.shortcuts import render_to_response
     323    from django import newforms as forms
     324
     325    class ContactForm(forms.Form):
     326        subject = forms.CharField(max_length=100)
     327        message = forms.CharField()
     328        sender = forms.EmailField()
     329        cc_myself = forms.BooleanField()
     330   
     331    def contact(request):
     332        if request.POST:
     333            f = ContactForm(request.POST)
     334            if f.is_valid:
     335                # ... do something with f.cleaned_data
     336        else:
     337            f = ContactForm()
     338        return render_to_response('contact.html', {'form': f})
     339
    316340Outputting forms as HTML
    317341------------------------
    318342
     
    389413    <p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>
    390414    <p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>
    391415
     416In a template, you can invoke this if the form has been handed into the context. For example:
     417
     418    {{ f.as_p }}
     419
     420
    392421``as_ul()``
    393422~~~~~~~~~~~
    394423
     
    405434    <li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>
    406435    <li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>
    407436
     437In a template, you can invoke this if the form has been handed into the context. For example:
     438
     439    {{ f.as_ul }}
     440
    408441``as_table()``
    409442~~~~~~~~~~~~~~
    410443
     
    421454    <tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>
    422455    <tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>
    423456
     457In a template, you can invoke this if the form has been handed into the context. For example:
     458
     459    {{ f.as_table }}
     460
     461which is the same as
     462
     463    {{ f }}
     464   
     465
    424466Configuring HTML ``<label>`` tags
    425467~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    426468
     
    602644    >>> str(f['subject'].errors)
    603645    ''
    604646
     647In the templates
     648----------------
     649
     650With the above example, lets put this into a view and show how you can use these
     651parts from the template designer's point of view. Assuming you start with a view like:
     652
     653    def contact(request):
     654        form = ContactForm()
     655        if request.method == 'POST':
     656            new_data = request.POST.copy()
     657            form = ContactForm(new_data)
     658            if form.is_valid():
     659                # do form processing here...
     660        return render_to_response('contact.html', {'form': form})
     661
     662Then you can have a simple template that uses the shortcuts for form.as_ul, form.as_p, or form.as_table. An example ``contact.html`` template::
     663
     664    <form method="POST">
     665    {{ form }}
     666    </form>
     667
     668or
     669
     670    <form method="POST">
     671    {{ form.as_table }}
     672    </form>
     673
     674If you wanted to work with the individual inputs of the form, you can either call out the fields directly, or iterate over them:
     675
     676    <form method="POST">
     677    <dl>
     678    {% for field in form %}
     679        <dt>{{ field.label }}</dt>
     680        <dd>{{ field }}</dd>
     681        <dd>{{ field.help_text }}</dd>
     682        {% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %}
     683    {% endfor %}
     684    </dl>
     685    </form>
     686
     687Alternately:
     688
     689    <form method="POST">
     690    <ul class="myformclass">
     691        <li>{{ form.sender.label }} {{ form.sender.label }}</li>
     692        <li class="helptext" >{{ form.sender.help_text }}</li>
     693        {% if form.sender.errors %}<ul class="errorlist">{{ form.sender.errors }}</dd>{% endif %}
     694
     695        <li>{{ form.subject.label }} {{ form.subject.label }}</li>
     696        <li class="helptext" >{{ form.subject.help_text }}</li>
     697        {% if form.subject.errors %}<ul class="errorlist">{{ form.subject.errors }}</dd>{% endif %}
     698
     699        ...
     700    </ul>
     701    </form>
     702
     703
    605704Subclassing forms
    606705-----------------
    607706
Back to Top