On Jun 18, 8:02 pm, chefsmart <moran.cors...@gmail.com> wrote:
> Hi,
<snip>
> Now consider the following view: -
>
> def edit_diploma(request, did):
>     diploma_to_edit = Diploma.objects.get(id=did)
>     if request.method == 'POST':
>         form = EditDiplomaForm(request.POST)
>         if form.is_valid():
>             diploma_to_edit.name = form.cleaned_data['name']
>             diploma_to_edit.short_name = form.cleaned_data
> ['short_name']
>             diploma_to_edit.is_active = form.cleaned_data['is_active']
>             diploma_to_edit.save()
>             return HttpResponseRedirect(reverse('drf-list_diplomas'))
>     else:
>         data = {'name': diploma_to_edit.name,
>                 'short_name': diploma_to_edit.short_name,
>                 'is_active': diploma_to_edit.is_active}
>         form = EditDiplomaForm(data)
>     return render_to_response('edit_diploma.html', {'form': form},
> context_instance=RequestContext(request))
>
> My edit_diploma.html template looks like the following: -
>
> <form method="post" action="." class="uniForm">
>     <fieldset class="blockLabels">
>         <legend>Edit diploma</legend>
>         {% for field in form %}
>             {% if field.errors %}
>                 <div class="ctrlHolder error">
>                 {% for error in field.errors %}
>                     <p class="errorField"><strong>{{ error }}</
> strong></p>
>                 {% endfor %}
>             {% else %}
>                 <div class="ctrlHolder">
>             {% endif %}
>                     {{ field.label_tag }}
>                     {{ field }}
>                     <p class="formHint">{{ field.help_text }}</p>
>                 </div>
>         {% endfor %}
>     </fieldset>
>     <div class="buttonHolder">
>         <button type="reset" class="resetButton">Reset</button>
>         <button type="submit" class="primaryAction">Submit</button>
>     </div>
> </form>
>
> When accessing the view via HTTP GET, the desired model instance's
> data is shown in the form for editing. However, my template has  {% if
> field.errors %} to accommodate different CSS for erroneous and error-
> free data. If I'm correct, this  {% if field.errors %} is causing
> validation to occur and thus I'm getting error messages in the
> rendered html even when accessing the view via HTTP GET.
>
> This of course is not desired. Could you please suggest a way to
> handle this? I need the validation and I need the conditional CSS as
> well.
>
> Thanks and Regards,
> CM.

The problem is here:

        data = {'name': diploma_to_edit.name,
                'short_name': diploma_to_edit.short_name,
                'is_active': diploma_to_edit.is_active}
        form = EditDiplomaForm(data)

It is the fact that you're passing this dictionary in as the 'data'
parameter, not the calling of is_errors in your template, which is
triggering validation. You need to pass it in as 'initial' instead -
if you do that, the form fields will be populated, but validation will
not take place.

        form = EditDiplomaForm(initial=data)

See 
http://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.initial
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to