烧瓶复选框和文本框问题

时间:2018-10-11 12:38:13

标签: python html flask

我正在创建一个需要登录的烧瓶表单,登录后将进入带有复选框和文本输入的输入表单。

我面临一个特定的问题:我无法获取文本框的值,但无法获取复选框的值。

我使用flask并使用每种请求方法来打印我的文本框,但没有获取值。

下面是我的主文件代码:

from flask import Flask, render_template
import os
from flask import redirect, url_for, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"]="sqlite:////OtrsSummary.db"
app.config["SECRET_KEY"]="thisiskey"

db = SQLAlchemy(app)

# @app.route("/ndex")
# def home():
#     names = os.getlogin().split(".")[0].title()
#     return render_template("index.html", name=names)

@app.route("/welcome", methods=['GET', 'POST'])
def welcome():
    if request.method=="POST":
        try:
            phase = request.form.get("phase")
            rphase = phase.replace("on", "1")
            print(rphase)
            sale = request.form.get("sale")
            rsale = sale.replace("on", "1")
            print(rsale)
            floor = request.form.get("floor")
            rfloor = floor.replace("on", "1")
            options = request.form.get("options")
            roptions = options.replace("on", "1")
            image = request.form.get("image")
            rimage = image.replace("on", "1")
            video = request.form.get("video")
            rvideo = video.replace("on", "1")
            possession = request.form.get("possession")
            rpossession = possession.replace("on", "1")
            amenities = request.form.get("amenities")
            ramenities = amenities.replace("on", "1")
            prdeactivation = request.form.get("prdeactivation")
            rprdeactivation = prdeactivation.replace("on", "1")
            np = request.form.get("np")
            rnp = np.replace("on", "1")
            newbooking = request.form.get("newbooking")
            rnewbooking = newbooking.replace("on", "1")
            bank = request.form.get("bank")
            rbank = bank.replace("on", "1")
            lat = request.form.get("lat")
            rlat = lat.replace("on", "1")
            usp = request.form.get("usp")
            rusp = usp.replace("on", "1")
            fact = request.form.get("fact")
            rfact = fact.replace("on", "1")
            prname = request.form.get("prname")
            rprname = prname.replace("on", "1")
            prdescription = request.form.get("prdescription")
            rprdescription = prdescription.replace("on", "1")
            prspecification = request.form.get("prspecification")
            rprspecification = prspecification.replace("on", "1")
            builderdetails = request.form.get("builderdetails")
            rbuilerdetails = builderdetails.replace("on", "1")
            tco = request.form.get("tco")
            rtco = tco.replace("on", "1")
            npdeactivation = request.form.get("npdeactivation")
            rnpdeactivation = npdeactivation.replace("on", "1")
            constuctionimages = request.form.get("constuctionimages")
            rconstuctionimages = constuctionimages.replace("on", "1")
            brochure = request.form.get("brochure")
            rbrochure = brochure.replace("on", "1")
            rera = request.form.get("rera")
            rrera = rera.replace("on", "1")
            rticketnumber = request.form["ticketnumber"]##here not getting the value
            rxidnumber = request.form["xidnumber"]##here not getting the value
            rreranumber = request.form["reranumber"]##here not getting the value
            print(rxidnumber)
            print(rticketnumber)
            msg = "Entry Submitted Successfully"

        except AttributeError:
            msg = "Please Do Not Submit Blank Form"
         #con.close()



    return render_template("same.html")

@app.route("/", methods=["GET", "POST"])
def log():
    names = os.getlogin().split(".")[0].title()
    error= None
    if request.method == "POST":
        if request.form["username"]!= os.getlogin() or request.form["password"]!="1234":
            error = "Invalid Credentials.Please Try again."
        else:
            return redirect(url_for("welcome"))
    return render_template("index.html", error=error, name=names)

app.run(debug=True)

这是我的html:

<!doctype <!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Page Title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> -->
        <!-- <script src="main.js"></script> -->
    </head>

    <body>
        <form action="" ALIGN="CENTRE" method="POST">
            <h3>OTRS Basic Information form</h3>
            <TABLE>
                <TR>
                    <TD>
                        Phases and Tower</TD>
                    <TD>
                        <input type="checkbox" name="phase">
                    </TD>
                    <TD>
                        Saleable Mapping </TD>
                    <TD>
                        <input type="checkbox" name="sale">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        Floor Plan </TD>
                    <TD>
                        <input type="checkbox" name="floor">
                    </TD>
                    <TD>
                        Options </TD>
                    <TD>
                        <input type="checkbox" name="options">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        Images </TD>
                    <TD>
                        <input type="checkbox" name="image">
                    </TD>
                    <TD>
                        Video </TD>
                    <TD>
                        <input type="checkbox" name="video">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        Possession Status/Date </TD>
                    <TD>
                        <input type="checkbox" name="possession">
                    </TD>
                    <TD>
                        Amenities </TD>
                    <TD>
                        <input type="checkbox" name="amenities">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        Project Deactivation </TD>
                    <TD>
                        <input type="checkbox" name="prdeactivation">
                    </TD>
                    <TD>
                        Np Slot Changes/Refresh </TD>
                    <TD>
                        <input type="checkbox" name="np">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        New Booking/Resale Lock </TD>
                    <TD>
                        <input type="checkbox" name="newbooking">
                    </TD>
                    <TD>
                        Bank
                    </TD>
                    <TD>
                        <input type="checkbox" name="bank">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        Lat Long/Location </TD>
                    <TD>
                        <input type="checkbox" name="lat">
                    </TD>
                    <TD>
                        USP </TD>
                    <TD>
                        <input type="checkbox" name="usp">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        Fact Table </TD>
                    <TD>
                        <input type="checkbox" name="fact">
                    </TD>
                    <TD>
                        Project Name </TD>
                    <TD>
                        <input type="checkbox" name="prname">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        Project Description </TD>
                    <TD>
                        <input type="checkbox" name="prdescription">
                    </TD>
                    <TD>
                        Project Specification </TD>
                    <TD>
                        <input type="checkbox" name="prspecification">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        Builder Details </TD>
                    <TD>
                        <input type="checkbox" name="builderdetails">
                    </TD>
                    <TD>
                        TCO/Payment Plan</TD>
                    <TD>
                        <input type="checkbox" name="tco">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        NP Deactivation </TD>
                    <TD>
                        <input type="checkbox" name="npdeactivation">
                    </TD>
                    <TD>
                        Construction Images </TD>
                    <TD>
                        <input type="checkbox" name="constuctionimages">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        Brochure </TD>
                    <TD>
                        <input type="checkbox" name="brochure">
                    </TD>
                    <TD>
                        Rera Available </TD>
                    <TD>
                        <input type="checkbox" name="rera">
                    </TD>
                </TR>
                <TR>
                    <TD>
                        Ticket Number </TD>
                    <TD><input type="text" name="ticketnumber">
                    </TD>
                    <TD>
                        XID Number </TD>
                    <TD>
                        <input type="text" name="xidnumber">
                    </TD>
                    <TD>
                        Rera Number </TD>
                    <TD>
                        <input type="text" name="reranumber">
                    </TD>
            </TABLE>
            <input type="submit" value="submit"><br>{{msg}}
        </form>
    </body>

</html>

有人可以提出一些解决方案吗?

2 个答案:

答案 0 :(得分:0)

实际上,我尝试运行您的代码,并且只有选中所有上述复选框,我才会获取文本框的值。

实际上,您正在捕获属性异常,但没有正确处理它。在您的代码中,如果未选中任何复选框,则会给出异常'NoneType'对象没有属性'replace '。不会被处理,因此将不会执行下一行代码。

我对使用复选框的建议是使隐藏类型具有相同的名称,并且值='off',因此,如果未选中它,则会给出off值。

<input type='hidden' name="checkbox_name" value="off">

答案 1 :(得分:0)

request.form 是一本词典,因此您可以检查您的复选框是否已被选中:

sale = 'checked' if 'sale' in request.form else 'not_checked'