form_is.valid为false(django)

时间:2017-04-03 11:25:09

标签: django-forms

我有以下代码,它继承自modelform的模型,但未能传递有效的条件。

Model.py

class contact(models.Model):
    fname = models.CharField(max_length=100)
    lname = models.CharField(max_length=100)
    email = models.EmailField()
    phone = models.IntegerField()
    message = models.CharField(max_length=100)

def __str__(self):
    return self.fname

urls.py

 urlpatterns = [
     url(r'^$', 'asp.views.index', name='root'),
     url(r'^contact/', 'asp.views.contact', name='contact'),
     url(r'^new$', 'asp.views.new_user', name='new'),
     url(r'^admin/', include(admin.site.urls)),
 ]

其中asp是我的应用程序

   def contact(request):
        return render(request, "contact.html", {})

    def new_user(request):
        print( '$' * 100)
        if request.method == 'POST':
            form = contactform(request.POST or None)
            print( '*' * 10)
            if form.is_valid():
                print( '!' * 10)
        else:
             form = contactform()
    return render(request, "index.html", {'form':form})

打印哪个$,*,但没有通过有效的条件。

forms.py

 class contactform(forms.ModelForm):
    class Meta:
        model = contact
        exclude = ()

以下是我的html页面

     <form class="form-horizontal" action="/new" method="post">{% csrf_token %}
            {{ form }}
                <fieldset>
                    <legend class="text-center header">Contact us</legend>
                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-1">
                            <input id="fname" name="name" type="text" placeholder="First Name" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-1">
                            <input id="lname" name="name" type="text" placeholder="Last Name" class="form-control">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-1">
                            <input id="email" name="email" type="text" placeholder="Email Address" class="form-control">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-1">
                            <input id="phone" name="phone" type="text" placeholder="Phone" class="form-control">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-1">
                            <textarea class="form-control" id="message" name="message" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7"></textarea>
                        </div>
                    </div>
                </fieldset>
            </form>

提前致谢。

0 个答案:

没有答案