Hi,

[ Please excuse if this post is a little verbose, but I want to
present my case thoroughly ]

I am using my own form to edit my models instead of using Django admin
(because of client compulsions)

Let's consider the following model: -

class Diploma(models.Model):
    short_name = models.CharField(max_length=20, unique=True)
    name = models.CharField(max_length=50, unique=True)
    is_active = models.BooleanField(db_index=True)

And the following Django form [ where STATUS_CHOICES = ((0,
'Inactive'), (1, 'Active')) ]: -

class EditDiplomaForm(forms.Form):
    short_name = forms.CharField(max_length=20)
    name = forms.CharField(max_length=50)
    is_active = forms.TypedChoiceField(coerce=int, empty_value=0,
choices=STATUS_CHOICES)
    def clean_short_name(self):
        short_name = self.cleaned_data['short_name']
        try:
            Diploma.objects.get(short_name=short_name)
        except ObjectDoesNotExist:
            return short_name
        raise forms.ValidationError('This short name is already in
use.')
    def clean_name(self):
        name = self.cleaned_data['name']
        try:
            Diploma.objects.get(name=name)
        except ObjectDoesNotExist:
            return name
        raise forms.ValidationError('This name is already in use.')

I have the two clean_ methods above to check whether short_name and
name are unique, as required in the model definition. (Not having
these checks might result in IntegrityError).

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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to