Newtt
Newtt

Reputation: 6190

Django ModelForm failing to save

I've got a Django model as follows:

models.py

class Property(models.Model):
    id = models.AutoField(unique=True, primary_key=True)
    name = models.CharField(max_length=500)
    desc = models.CharField(max_length=2000)
    address_line1 = models.CharField(max_length=200)
    address_line2 = models.CharField(max_length=200)
    locality = models.CharField(max_length=200, default=0)
    city = models.CharField(max_length=200)
    state = models.CharField(max_length=200)
    pin = models.BigIntegerField(max_length=200)
    phone = models.BigIntegerField(max_length=20)
    secondary_number = models.BigIntegerField(max_length=20, blank=True)
    image = models.ImageField(upload_to='dynamic/img')
    square_feet = models.BigIntegerField(max_length=200, default=0)
    price = models.BigIntegerField(max_length=200, default=0)
    NO = 'NO'
    YES = 'YES'
    APPROVAL = ((NO, 'no'), (YES, 'yes'))
    active = models.CharField(choices=APPROVAL, default=NO, max_length=3)

    def __unicode__(self):
        return self.name

A modelform for this:

class PostForm(forms.ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter name for Property'}), required=True)
    desc = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control input-area', 'placeholder': 'Enter a small description', 'rows': '5', 'style': 'resize:none;'}), required=True)
    address_line1 = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter first line of address'}), required=True)
    address_line2 = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter second line of address'}), required=True)
    locality = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter locality'}), required=True)
    city = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter city name'}), required=True)
    state = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter state name'}), required=True)
    pin = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter pincode'}), required=True)
    phone = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter contact number'}), required=True)
    secondary_number = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter secondary contact number'}), required=False)
    image = forms.FileInput()
    square_feet = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter area of property in square feet'}), required=True)
    price = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control input-area', 'placeholder': 'Enter price of property in Rupees'}), required=True)
    NO = 'NO'
    YES = 'YES'
    APPROVAL = ((NO, 'no'), (YES, 'yes'))
    active = forms.CharField(widget=forms.HiddenInput(), initial=NO)

    class Meta:
        model = Property

and my views.py for this is as follows:

def postproperty(request):
    context = RequestContext(request)
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            form.save(commit=True)
            return HttpResponseRedirect(reverse('postthanks'))
        else:
            print form.errors

    else:
        form = PostForm()

    content = {'form': form}
    return render_to_response('postproperty.html', content, context)

the template contains this:

<form id="property_form" method="POST" action="">
    {% csrf_token %}
    {% if form.errors %}
        <p style="color: red;">
            Please correct the error{{ form.errors|pluralize }} below.
        </p>
    {% endif %}
    {% for hidden in form.hidden_fields %}
        {{ hidden }}
    {% endfor %}
    {% for field in form.visible_fields %}
        {{ field.help_text}}
        {{ field }}
    {% endfor %}
    <input type="submit" name="submit" value="Add Property" class="btn btn-info test-button" style="margin-top:20px;"/>
</form>

The form is rendered correctly in the template. When I go to save the form from the template, it gives me Please correct the error below. No error is specified and I'm lost as to which field seems to be giving me an error. Help would be appreciated!

EDIT: As Daniel said, I added {{ field.errors }} and {{ form.non_field_errors }} to the template. It shows error in the image field. Why would this be?

Upvotes: 0

Views: 98

Answers (2)

Newtt
Newtt

Reputation: 6190

Needed to add this to the template:

<form enctype="multipart/form-data" method="post" action="/foo/">

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599490

Well, you're not showing any of the errors in the template. Along with each field and label, you should add {{ field.errors }}, plus {{ form.non_field_errors }} at the to of the form.

Upvotes: 1

Related Questions