• AddDemo核心代码


    gcodeform.py代码:

    from wtforms import Form,StringField,IntegerField,SubmitField
    from wtforms.validators import DataRequired,NumberRange
    class AddForm(Form):
        iFirst=IntegerField('First',validators=[DataRequired(),NumberRange(min=1,max=10000,message="该值取值区间在1-10000")])
        iSecond=IntegerField('Second',validators=[DataRequired(),NumberRange(min=1,max=10000,message="该值取值区间在1-10000")])
        iSum=IntegerField('Sum')
        submit=SubmitField("提交")

    main代码:

    from Lib import gcodeform
    from wtforms import Form,IntegerField,SubmitField
    from wtforms.validators import DataRequired,Length
    from flask import Flask, render_template, request,url_for,flash
    app = Flask(__name__)
    app.secret_key="12345" 
    @app.route('/')
    @app.route('/Index',methods = ['POST','GET'])
    def Index():    
        if request.method=='GET':
            f1=gcodeform.AddForm() 
            iSum=11         
            return render_template('Index.html',form=f1,iSum=iSum)
        else:
            f2=gcodeform.AddForm(request.form)     
            if f2.validate():          
                iFirst=f2.iFirst.data
                iSecond=f2.iSecond.data
                iSum=iSecond+iFirst
                f2.iSum=iSum
                return render_template('Index.html',form=f2)
            else:
                flash("请输入整数数值,范围在1-10000之间.")
                return "err"
    
    if __name__ == '__main__':
       app.run(debug = False,port=6669)

    html:

    <!DOCTYPE html>
    <html>
    <head>
        <title>求和</title>
    </head>
    <body>
    <h2>求和</h2>
    <form action="/Index" method="POST">
        {{form.csrf_token}}    
        {{form.iFirst.label}}{{form.iFirst}}+{{form.iSecond.label}}{{form.iSecond}}={{form.iSum.label}}{{form.iSum}}<br>
        {{form.submit}}<br>
    </form>
    {% for message in get_flashed_messages() %}
            {{ message }}
    {% endfor %}
    </body>
    </html>

    升级版:

    main.py

    from Lib import gcodeform
    # from wtforms import Form,IntegerField,SubmitField
    # from wtforms.validators import DataRequired,Length
    from flask import Flask, render_template, request,url_for,flash
    app = Flask(__name__)
    app.secret_key="12345"
    @app.route('/',methods = ['POST','GET'])
    def Index():    
        if request.method=='GET':
            f1=gcodeform.AddForm()
            f1.iFirst.data=0
            f1.iSecond.data=0 
            iSum=0 
            flash("美好的一天.")             
            return render_template('Index.html',form=f1,iSum=iSum)
        else:
            f2=gcodeform.AddForm(request.form)     
            if f2.validate():          
                iFirst=f2.iFirst.data
                iSecond=f2.iSecond.data
                iSum=iSecond+iFirst
                if(iFirst==0 and iSecond==0):  
                    flash("你是一个无聊的人.")
                else:
                    flash("OK")
                    flash("加油!")                   
                return render_template('Index.html',form=f2,iSum=iSum)
            else:            
                return "err,请联系管理员"
    if __name__ == '__main__':
       app.run(debug = True,port=9991)

    gcodeform.py

    from wtforms import Form,StringField,IntegerField,SubmitField
    from wtforms.validators import InputRequired,NumberRange
    class AddForm(Form):
        iFirst=IntegerField('First',validators=[InputRequired(),NumberRange(min=0,max=100,message="该值取值区间在0-100")])
        iSecond=IntegerField('Second',validators=[InputRequired(),NumberRange(min=0,max=100,message="该值取值区间在0-100")])
        submit=SubmitField("提交")

    Index.html:

    <!DOCTYPE html>
    <html>
    <head>
        <title>求和</title>
    </head>
    <body>
    <h2>求和</h2>
    <form action="/" method="POST">
        {{form.csrf_token}}    
        {{form.iFirst.label}}{{form.iFirst}}+{{form.iSecond.label}}{{form.iSecond}}=<span>{{iSum}}</span><br><br>
        {{form.submit}}<br>
        <hr>        
    </form>
    {% for message in get_flashed_messages() %}
        <div>{{message}}</div>
     {% endfor %}
    </body>
    </html>

     升级版:

    gcodeform.py

    from wtforms import Form,StringField,IntegerField,SubmitField
    from wtforms.validators import InputRequired,NumberRange
    class AddForm(Form):
        iFirst=IntegerField('First',validators=[InputRequired(),NumberRange(min=0,max=100,message="Must be in 1-100")], render_kw={"required":False,"class":"form-control","placeholder": "请输入数值"},default=1)
        iSecond=IntegerField('Second',validators=[InputRequired(),NumberRange(min=0,max=100,message="Must be in 1-100")],render_kw={"required":False,"class":"form-control", "placeholder": "请输入数值"},default=2)
        submit=SubmitField("提交")

    indexc.html

    <!DOCTYPE html>
    <html>
    <head>
        <title>求和</title>
         <link rel="stylesheet" href="{{url_for('static',filename='bootstrap4/css/bootstrap.css')}}" type="text/css">
    </head>
    <body>
    <div class="container mt-2">
    <h2>求和</h2>
    <form id="form" action="/" method="POST">
        {{form.csrf_token}}    
        {{form.iFirst.label}}{{form.iFirst}}{{form.iSecond.label}}{{form.iSecond}}<div>{{iSum}}</div><br><br>
      {{form.submit}}
        <br>
        <hr>        
    </form>
    <div id="fronttip" class="alert alert-info alert-dismissable">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">
    &times;
    </button>
    <span id="spantip"></span>
    </div>
    <div id="backtip">
    {% for m in get_flashed_messages() %}
    <div  class="alert alert-info alert-dismissable">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">
    &times;
    </button>
      {{m}}
       </div>
     {% endfor %}
    </div>
    </div>
    <script type="text/javascript" src="{{url_for('static',filename='jquery/jquery-3.3.1.js')}}"></script>
    <script type="text/javascript" src="{{url_for('static',filename='bootstrap4/js/bootstrap.js')}}"></script>
    <script>
            $(function () { 
                var tip=$("#spantip").html()
                if(tip=="") $("#fronttip").hide()
                else    $("#fronttip").show()             
              $("#submit").click(function (event) {
                var a = $("#iFirst").val();
                var b = $("#iSecond").val();
                // $("#fronttip").hide()
                $("#backtip").hide()                 
                if(a=="0"&&b=="0")
                {
                  $("#spantip").html("前端:你是一个无聊的人。")
                  $("#fronttip").show()
                  $("#backtip").show()            
                  return  true
                }
                else if(a=="4"||b=="4")
                {
               
                  $("#spantip").html("前端:数字不吉利,禁止提交.")
                  $("#fronttip").show()                     
                  return  false  
                }
                else{
                 $("#spantip").html("前端:OK。")
                 $("#fronttip").show()
                  $("#backtip").show()     
                  return  true          
                }
              })       
            });   
          </script>
    </body>
    </html>

    mc.py

    from Lib import gcodeform2
    # from wtforms import Form,IntegerField,SubmitField
    # from wtforms.validators import DataRequired,Length
    from flask import Flask, render_template, request,url_for,flash
    app = Flask(__name__)
    app.secret_key="12345"
    @app.route('/',methods = ['POST','GET'])
    def Index():    
        if request.method=='GET':
            f1=gcodeform2.AddForm()
            # f1.iFirst.data=0
            # f1.iSecond.data=0 
            iSum=3 
            flash("后端:美好的一天.")             
            return render_template('Indexc.html',form=f1,iSum=iSum)
        else:
            f2=gcodeform2.AddForm(request.form)     
            if f2.validate():          
                iFirst=f2.iFirst.data
                iSecond=f2.iSecond.data
                iSum=iSecond+iFirst
                if(iFirst==0 and iSecond==0):  
                    flash("后端:你是一个无聊的人.")
                else:
                    flash("后端:OK")
                    flash("后端:加油!")                   
                return render_template('Indexc.html',form=f2,iSum=iSum)
            else:            
                return "err,请联系管理员"
    if __name__ == '__main__':
       app.run(debug = True,port=9997)
  • 相关阅读:
    Oracle
    CCF 201609-5 祭坛
    prometheus同时执行多个查询
    压力测试 Apache ab
    kubernetes reference
    python 深拷贝
    [ argo workflow ]
    django orm 改动数据库中已存在的表(添加、删除、修改表字段)migrations
    内存压力测试命令
    django 未成功初始化自定义表单
  • 原文地址:https://www.cnblogs.com/exesoft/p/16333050.html
Copyright © 2020-2023  润新知