如何从flask-wtf表单中获取数据到Firebase实时数据库?

时间:2019-12-20 13:52:14

标签: python firebase flask firebase-realtime-database pyrebase

大家好,我正在一个简单的网站上进行工作,该网站使用flask-wtf和fireebase使用pyrebase库收集信息

 <form action="{{ url_for('hello')}}" method="post" enctype="multipart/form-data"> 
    {{ form.hidden_tag() }}
    <h2>Tell us what do you want</h2>
        {{ form.item.label }} {{form.item() }} <br>
        {{ form.cost.label }} {{form.cost() }} <br>
        {{ form.time.label }} {{form.time() }} <br>
    <h2>We would love to know about you</h2>
        {{ form.name.label }} {{form.name() }} <br>
        {{ form.age.label }} {{form.age() }} <br>
        {{ form.user_address.label }} {{form.user_address() }} <br>
        {{ form.email.label }} {{ form.email() }} <br>
        {{ form.birthday.label }} {{ form.birthday() }} <br>
        {{ form.status.label }} {{ form.status() }} <br>

    <h2>Tell us about your employment </h2>
    {{ form.employer.label }} {{ form.employer() }} <br>
    {{ form.address.label }} {{ form.address() }} <br>
    {{ form.contract.label }} {{ form.contract() }} <br>
    {{ form.occupation.label }} {{ form.occupation() }} <br>

    <h2>Tell us about your finances</h2>

    {{ form.salary.label }} {{ form.salary() }} <br>
    {{ form.loan.label }} {{ form.loan() }} <br>
    {{ form.dependents.label }} {{ form.dependents() }} <br>
    {{ form.expenditures.label }} {{ form.expenditures() }} <br>

    {{ form.submit() }}

那是表格,这里是视图

import os
import pyrebase
from forms import ApplicationForm
from flask import Flask, render_template, redirect, request


config = {
   "apiKey": "AIzaSyDFNhK5NczJjHQK7v2i5wnk1DNocRSMMIQ",
   "authDomain": "moto-e92ef.firebaseapp.com",
   "databaseURL": "https://moto-e92ef.firebaseio.com",
   "projectId ": "moto-e92ef",
   "storageBucket": "moto-e92ef.appspot.com",
   "messagingSenderId": "774472119075",
   "appId": "1:774472119075:web:61e9b703590cad30bc1296",
   "measurementId": "G-ZPPWSCN3CH"
}

firebase = pyrebase.initialize_app(config)

db = firebase.database()


storage = firebase.storage()



app = Flask(__name__)
app.secret_key = os.urandom(24)


@app.route('/', methods=['GET', 'POST'])
def index():
   return render_template('index.html', title="LIPASWYFT | SHOP NOW, PAY LATER")


@app.route("/apply", methods=['GET', 'POST'])
def hello():

   form = ApplicationForm()

   if request.method == 'POST' and form.validate():


       item = request.form['item']
       cost = request.form['cost']
       time = request.form['time']
       name = request.form.get('name')
       age = request.form.get('age')
       user_address = request.form.get('user_address')
       email = request.form.get('email')
       phone = request.form.get('phone')
       birthday = request.form.get('birthday')
       status = request.form.get('status')
       employer = request.form["employer"]
       address = request.form["address"]
       contract = request.form["contract"]
       occupation = request.form["occupation"]
       salary = request.form["salary"]
       loan = request.form["loan"]
       dependents = request.form["dependents"]
       expenditures = request.form["expenditures"]



       data = {
           "item": item,
           "cost" : cost,
           "time" : time,
           "name" : name,
           "age" : age,
           "user_address" : user_address,
           "email" : email,
           "phone" : phone,
           "birthday" : birthday,
           "status" : status,
           "employer" : employer,
           "address" : address,
           "contract" :  contract,
           "occupation" : occupation,
           "salary " : salary,
           "loan" : loan,
           "dependents" : dependents,
           "expenditures" : expenditures
       }

       db.child("requests").push(data)
       return render_template('apply.html', title='Buy Now', form = form)

   return render_template('apply.html', form = form)



if __name__ == '__main__':
   app.run(debug=True)

当我发布无法在Firebase中反映的表单时,form.py火是

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, IntegerField
from wtforms.validators import DataRequired, Email


class ApplicationForm(FlaskForm):
    item = StringField('Item you want to buy', validators=[DataRequired(message="Please fill in the item")])
    cost = IntegerField('Cost', validators=[DataRequired(message="Please enter the cost")])
    time = StringField('Repayment time', validators=[DataRequired(message="Enter your repayment time")])
    name = StringField('Enter your name', validators=[DataRequired(message="Please enter your name")])
    age  = StringField('Your age', validators=[DataRequired(message="Enter your age")])
    user_address =StringField("Your address", validators=[DataRequired(message="Please tell us your address")])
    email = StringField("Your email", validators=[DataRequired(), Email(message="Enter valid email")])
    phone = IntegerField("Enter your phone number", validators=[DataRequired(message="Enter valid number")])
    birthday = StringField("Enter your birthdate", validators=[DataRequired(message="enter your birthday")])
    status = StringField("Marital status", validators=[DataRequired(message="Please fill in your status")])
    employer = StringField("Employer", validators=[DataRequired(message="Please fill the name of your employer")])
    address = StringField("Employer's address", validators=[DataRequired(message="enter the address of your employer"), ])
    contract = StringField("Contract", validators=[DataRequired(message="Please fill in the terms of your contract"), ])
    occupation = StringField("Occupation", validators=[DataRequired(message="Please enter your occupation")])
    salary = IntegerField("Salary", validators=[DataRequired( message="Please enter your salary")])
    loan = IntegerField("Any outstanding loan", validators=[DataRequired(message="Please fill this in")])
    dependents = IntegerField("Any dependents", validators=[DataRequired(message="Please fill this in")])
    expenditures = IntegerField("Expenditures", validators=[DataRequired(message="please fill this in")])
    submit = SubmitField("BUY")




如何从wtf中获取数据并将其发布到我的Firebase数据库中?

0 个答案:

没有答案