mysql item.pop_python flas中mysql的popluate组合框值

以下是解决方案:

在运行页面.html文件:

Form

$(function() {

var dropdown = {

customer: $('#select_customer'),

product: $('#select_product')

};

// function to call XHR and update product dropdown

function updateproduct() {

var customer = dropdown.customer.val();

dropdown.product.attr('disabled', 'disabled');

console.log(customer);

if (customer.length) {

dropdown.product.empty();

$.getJSON("{{ url_for('productChange') }}", {customer: customer}, function(data) {

console.log(data);

data.forEach(function(item) {

dropdown.product.append(

$('', {

value: item.vals,

text: item.vals

})

);

});

dropdown.product.removeAttr('disabled');

});

}

}

// event listener to customer dropdown change

dropdown.customer.on('change', function() {

updateproduct();

});

});

{{ form.customer}}

{{ form.product }}

在数据库连接.py在

^{pr2}$

在应用程序副本文件:from flask import *

import os

from dbconnect import connection

from flask import Flask, render_template, flash, request, url_for, redirect, session,Response

from wtforms import Form, BooleanField, TextField, PasswordField, validators,SelectField

from passlib.hash import sha256_crypt

from MySQLdb import escape_string as thwart

#from flask_sqlalchemy import SQLAlchemy

from flask_wtf import FlaskForm

from wtforms_sqlalchemy.fields import QuerySelectField

from urllib.request import *

from flask import json

import gc

app = Flask(__name__)

app.secret_key = 'my unobvious secret key'

class runpageForm(Form):

customer = SelectField('Select your customer Name', choices=[], id='select_customer')

product = SelectField('Select your product Name', choices=[], id='select_product')

@app.route('/runpage/', methods=['GET', 'POST'])

def runpage():

try:

form = runpageForm()

if request.method == "POST" :

# loading values in SelectFields on page load

for row in fetch_customer('custdetail', 'customer'):

customer = str(row[0])

form.customer.choices += [(customer, customer )] ;

#value_customerqq = dict(form.customer.choices).get(form.customer.data)

return render_template("runpage.html", form=form)

except Exception as e:

error = "Invalid credentialsw, try again."

return(str(e))

@app.route('/productChange/', methods=['GET', 'POST'])

def productChange():

try:

form = runpageForm()

if request.method == "GET":

customer = request.args.get('customer')

print(customer)

c, conn = connection()

c.execute("SELECT product FROM custdetail WHERE customer = %s", [customer])

product = c.fetchall()

print(product)

data = [{"vals": x[0]} for x in product]

print ("hello tript")

print (data)

c.close()

return jsonify(data)

except Exception as e:

error = "Invalid customer name, try again."

return(str(e))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值