How to build a URL with commas to query with filters using flask-wtforms jinja QuerySelectField and python operators “eq,gt,lt,et”

情到浓时终转凉″ 提交于 2020-01-06 04:45:09

问题


This is a flask front end to an Accounting database.

I am struggling to create a form to pass a query back to veiw function, I need to query one or more columns out of eleven columns at once, I have the code for the backend side after somebody helped me here but I dont know how to build a URL like this:

books?filter=aux,eq,FILCUI&credit,eq,445663

where eq are operators from here. These I can use as a dropdown list to have "eqaul too" "greater than" "not eqaul". Brilliant

Six of my eleven columns have relationships with other tables, when I add a new row with a form I use QuerySelectField to have the correct entries, I often need to add or update the five parent tables so using QuerySelectField is a must. My code for the form is this:

    <div class="AUX">
        <form action="{{ url_for('books.books', filter=selectaux) }}" method="GET">
            {{ aux_form.selectaux(class="input") }}{{ aux_form.submitaux(class="submit") }}
        </form>
    </div>

which out puts this:http://192.168.0.45/books?selectaux=ATTMAR&submitaux=submit

I have looked at these :- flask creating urls multiple parameters and dynamic urls But the answers arent obvious enough for me, sorry! Also I am using "GET" as the method, is this write? here is my veiw function.

@bp.route("/books", methods=["GET"])
def books():
    args = flask.request.args
    filters = create_filters(args.get("filter"))
    query = Book.query.order_by(Book.date.desc())
    query = filter_query(filters, query, Book)
    aux_form = SearchAuxForm()
    add_form = AddBookForm()
    books = []
    def get_books(offset=0,per_page=100):
        return books[offset: offset + per_page]
    for book in query.all():
        books.append(dict(
            id=book.id,
            date=book.date,
            description=book.description,
            debit=book.debit,
            credit=book.credit,
            montant=book.montant,
            AUX=book.AUX,
            TP=book.TP,
            REF=book.REF,
            JN=book.JN,
            PID=book.PID,
            CT=book.CT
        ))
    page, per_page, offset = get_page_args(page_parameter='page',
                                       per_page_parameter='per_page')
    total = len(books)
    pagination_books = get_books(offset=offset,per_page=per_page)
    pagination = Pagination(page=page, per_page=per_page, total=total)
    return render_template('books/books.html', title='Books', page=page, books=pagination_books,
                       per_page=per_page, pagination=pagination, aux_form=aux_form, add_form=add_form)

all help or ideas much neede to help this simpleton!

EDIT: Here is my code for AuxSearchForm followed by How I imagine the form might look like:

class SearchAuxForm(FlaskForm):
    selectaux = QuerySelectField('Aux', query_factory=AUX, get_label='id')
    submitaux = SubmitField('submit')

class SearchForm(FlaskForm)
    date = DateField('date', validators=[DataRequired()])
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    description = StringField('Description', validators=[DataRequired()])
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    debit = QuerySelectField('Debit', query_factory=debit, get_label='id')
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    credit = QuerySelectField('Credit', query_factory=credit, get_label='id')
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    montant = FloatField('Montant', validators=[DataRequired()])
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    AUX = QuerySelectField('Auxilliare', query_factory=AUX, get_label='id')
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    TP = QuerySelectField('Type', query_factory=TP, get_label='id')
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    REF = StringField('REF')
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    JN = QuerySelectField('JN', query_factory=JN, get_label='id')
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    PID = QuerySelectField('PID', query_factory=PID, get_label='id')
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    CT = IntegerField('CT')
    operator = SelectField('op', choices=['eq', 'eq'), ('ne','ne'), ('lt', 'lt'), ('gt', 'gt'), ('le', 'le'), ('ge','ge')
    submit = SubmitField('submit')

This second form (I only intend to have one form) will enable me to query each row for a value "operator", and also query two or more rows in the same manner, I hope that is understandable! Warm regards Paul

来源:https://stackoverflow.com/questions/59520072/how-to-build-a-url-with-commas-to-query-with-filters-using-flask-wtforms-jinja-q

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!