Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 改进Flask中的视图功能_Python_Flask_Sqlalchemy_Flask Sqlalchemy - Fatal编程技术网

Python 改进Flask中的视图功能

Python 改进Flask中的视图功能,python,flask,sqlalchemy,flask-sqlalchemy,Python,Flask,Sqlalchemy,Flask Sqlalchemy,下面是我正在制作的页面示例。在本例中,我有两个select字段,用于在按下submit按钮时过滤右侧的结果 我解决问题的方法是使用if elif,但是如果我有更多的过滤器,我的代码将很难管理。在本例中,我有2个过滤器,这意味着要管理4个distint查询,如果我有4个过滤器,这将意味着16个不同的查询 你能给我一个更好的方法,而不是我的方法吗 我的代码的基本部分: 模型 类型 看法 查看功能将根据选择字段显示查询结果。在这种情况下: 如果用户在“标准”和“组”字段中选择“全部”,则视图将执行

下面是我正在制作的页面示例。在本例中,我有两个select字段,用于在按下submit按钮时过滤右侧的结果

我解决问题的方法是使用
if elif
,但是如果我有更多的过滤器,我的代码将很难管理。在本例中,我有2个过滤器,这意味着要管理4个distint查询,如果我有4个过滤器,这将意味着16个不同的查询

你能给我一个更好的方法,而不是我的方法吗

我的代码的基本部分: 模型 类型 看法 查看功能将根据选择字段显示查询结果。在这种情况下:

  • 如果用户在“标准”和“组”字段中选择“全部”,则视图将执行standard.query

  • 如果用户仅在标准字段中选择“全部”,则视图将仅过滤组列

  • 如果用户在“组”字段中选择“全部”,则视图将仅过滤标准列

  • 在thoses字段中选择另一个选项将用于筛选查询

    @app.route("/", methods=['GET', 'POST'])
    def index():
        form = StandardForm()
        if form.standard.data == "All" and form.group.data == "All":
            q = Standard.query
        elif form.standard.data == "All":
            q = Standard.query.filter(
                Standard.group == form.group.data)
        elif form.group.data == "All":
            q = Standard.query.filter(
                Standard.stdname.has(name=form.standard.data))
        else:
            q = Standard.query.filter(
                Standard.stdname.has(name=form.standard.data),
                Standard.group == form.group.data)
    
        return render_template(
            'index.html',
            form=form,
            standards=q.all())
    

我想出了一种更通用的方法来解决这个问题

@app.route("/", methods=['GET', 'POST'])
def index():
    form = StandardForm()

    std = form.data.get('standard')
    group = form.data.get('group')

    q = Standard.query
    for fieldname, value in form.data.items():
        if fieldname == 'standard' and value != "All":
            q = q.filter(Standard.stdname.has(name=std))
        elif fieldname == 'group' and value != "All":
            q = q.filter(Standard.group == group)

    return render_template(
        'index.html',
        form=form,
        standards=q.all())

这可能更适合于每个表单都没有自己的视图的原因吗?是的,你是对的,版主可以从站点进行此更改吗?
@app.route("/", methods=['GET', 'POST'])
def index():
    form = StandardForm()
    if form.standard.data == "All" and form.group.data == "All":
        q = Standard.query
    elif form.standard.data == "All":
        q = Standard.query.filter(
            Standard.group == form.group.data)
    elif form.group.data == "All":
        q = Standard.query.filter(
            Standard.stdname.has(name=form.standard.data))
    else:
        q = Standard.query.filter(
            Standard.stdname.has(name=form.standard.data),
            Standard.group == form.group.data)

    return render_template(
        'index.html',
        form=form,
        standards=q.all())
@app.route("/", methods=['GET', 'POST'])
def index():
    form = StandardForm()

    std = form.data.get('standard')
    group = form.data.get('group')

    q = Standard.query
    for fieldname, value in form.data.items():
        if fieldname == 'standard' and value != "All":
            q = q.filter(Standard.stdname.has(name=std))
        elif fieldname == 'group' and value != "All":
            q = q.filter(Standard.group == group)

    return render_template(
        'index.html',
        form=form,
        standards=q.all())