Python Flask WTForms Dependent下拉列表

2024-09-27 00:21:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有人能帮我。在

我想能够点击客户和地点的基础上的某些客户,作为一个依赖下拉列表。此信息来自数据库,因此下面的代码中有查询。在

这是我对客户和位置的表单功能

class CustomerPick(SubForm):
    customer = QuerySelectField(u'Customer',
                            get_label=u'sCustomer',
                            query_factory=lambda : 
                            (TCustomer.query.order_by(TCustomer.sCustomer)),
                            validators=[DataRequired(),])
    location = QuerySelectField(u'Location',
                            get_label=u'sLocation',
                            query_factory=lambda : 
                            (TLocation.query.order_by(TLocation.sLocation)),
                            validators=[DataRequired(),])

这是视图部分

^{pr2}$

这是一个下拉列表的图片也供参考,如果有任何其他需要让你们尝试一下,请让我知道。提前谢谢! Photo


Tags: lambda列表getby客户factoryorderquery
1条回答
网友
1楼 · 发布于 2024-09-27 00:21:23

我不太明白你的问题,但你希望能够点击一个用户,并根据位置填充下拉列表?在

这涉及到一些Ajax来回发送数据。 我将给你一个代码片段的最小化版本(未测试)。在

 // front-end - this handles user behavior in dropdown. When a user changes a value
 // in a user drop down, it will send a request to your Flask view function.
 $("#user_drop_down").change(function () {
        let user_identifier = this.value;
        $.ajax({
            type: "GET",
            url:/url_to_flask_view_function/,
            data: {user_identifier: user_identifier},
            success: function (resp) {
                $('#your_designated_div_for_both_dropdowns_div').html(resp.data)
            }
        });
    });

# back-end - this receives the request sent from front-end and process the Flask-WTF
# form options so that it can render different options in the dropdowns. In this view
# function, we will return jsonified template with newly processed form_object
# instead of rendering the option data. You can return the json with only the data
# but it involves more javascript parsing and may be difficult for you.
@app.route('/url_to_flask_view_function/')
def form_processing():
    user_identifier = request.args.get('user_identifier)
    # now we've gotten the user_identifier, you will need to query. Below query is totally made up.
    query_to_where_location = Location.query.filter(Location.user_identifier= user_identifier).first()
   # your query result will not be in a tuple format
   # if your query result is like this "locA, locB, locC, locD", you need to process
   # it so that you make [('locA', 'locA'), ('locB', 'locB').......] 
   form_object = MyForm()

    form_object.location.choices = processed_list
    return jsonify({"data":render_template('template_that_contains_your_drodpdowns.html',
                                   form_obj=form_obj)})

<!  HTML piece, you should've had this already but make sure to specify your fields in HTML following Jinja2 synthax. >
<form>
{{form_object.user}}
{{form_object.dropdown}}
</form>

总之,这里的想法是使用.change捕捉用户行为,然后根据更改,将带有用户标识符的请求发送到服务器端。一旦它到达服务器端,您将在数据库中进行查询,并使用不同的处理表单再次呈现相同的模板。在

最好的方法是,一旦将user_标识符放入视图中,进行查询并返回jsonified location对象,然后在成功块中更改下拉输入元素的。在

如果你有更多问题请告诉我。在

相关问题 更多 >

    热门问题