简体   繁体   English

如何使用龙卷风在前端显示错误

[英]how tornado can be used to show error on frontend

Here is my python file for the contact form of the website. 这是网站联系表格的python文件。 the main file is getting the data and sending it to the database. 主要文件是获取数据并将其发送到数据库。

 #main.py
    class next(tornado.web.RequestHandler):
    def get(self):
        self.render("contact.html")
    async def post(self):
        form = contact(self.request.arguments)
        name_ = form.data["name"]
        phone_ = form.data["phone"]
        email_ = form.data["email"]
        message_ = form.data["message"]
        image_ = form.data["pic"]
        print(name_, phone_, email_, message_, image_)

        try:

            dbsend = TestModel.create(name=name_, phone=phone_, email=email_,message=message_,image=image_)
            dbsend.save()
            print('done')
            self.render("contact.html")
        except:
            print('not done')

this is the file where I think all validation code belongs 这是我认为所有验证代码都属于的文件

validation.py validation.py

from wtforms import *
from wtforms_tornado import Form
class contact(Form):
    name= StringField('name',[validators.required()])
    phone = StringField('phone', [validators.Length(min=4, message="too Short")])
    email = StringField('email', [validators.Length(min=4, message="too Short")])
    message = StringField('message', [validators.Length(min=4, message="too Short")])
    pic = StringField('pic', [validators.Length(min=4, message="too Short")])

This is the HTML page containing the form. 这是包含表单的HTML页面。

contact.html contact.html

 <div class="row">
    <div class="col-lg-8 mb-4">
      <h3>Send us a Message</h3>
      <form name="sent" action="/contact" method="post" id="cForm" >
        <div class="control-group form-group">
          <div class="controls">
            <label>Full Name:</label>
            <input type="text"  name="name" >
            <p class="help-block"></p>
          </div>
        </div>
        <div class="control-group form-group">
          <div class="controls">
            <label>Phone Number:</label>
            <input  name="phone" >
          </div>
        </div>

        <div class=" ">
          <div class="">
            <label>Email Address:</label>
            <input   name="email">
          </div>
        </div>
        <div class="control-group form-group">
          <div class="controls">
            <label>Message:</label>
            <textarea rows="10" cols="100"  name="message" ></textarea>
          </div>
        </div>
        <div class="control-group form-group">
          <div class="controls">
            <label>Image:</label>
            <input type="file" name="pic" accept="image/*">
          </div>
        </div>
        <div id="success"></div>
        <!-- For success/fail messages -->
        <button type="submit" class="btn btn-primary" id="sendMessageButton">Send Message</button>
      </form>

    </div>

How can I put validations on email phone etc and show the error on the front end? 如何在电子邮件电话等上进行验证,并在前端显示错误?

As it all stated it WTForms documentation, you can use validate() method of a form-instance to run validators. 正如WTForms文档所述, 您可以使用form-instance的validate()方法来运行验证器。 All errors would be in the form.errors attribute. 所有错误都将在form.errors属性中。 You can insert errors messages into a HTML page by using a templating engine. 您可以使用模板引擎将错误消息插入HTML页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM