Django ModelForm无法保存

时间:2014-05-27 22:16:14

标签: python django

我有一个Django模型如下:

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

此模型:

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

我的views.py如下:

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)

模板包含:

<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>

表单在模板中正确呈现。当我从模板中保存表单时,它会给我Please correct the error below。没有指定错误,我丢失了哪个字段似乎给我一个错误。 帮助将不胜感激!

编辑: 正如丹尼尔所说,我在模板中添加了{{ field.errors }}{{ form.non_field_errors }}。它在图像字段中显示错误。为什么会这样?

2 个答案:

答案 0 :(得分:1)

好吧,您没有在模板中显示任何错误。除了每个字段和标签外,您还应在表单的地址添加{{ field.errors }}{{ form.non_field_errors }}

答案 1 :(得分:1)

需要将其添加到模板中:

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