[Flask] Query in relation to having two forms on the same page and sorting tables.

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Wed Aug 7 08:27:25 EDT 2019


All,

 

I am using flask_wtf to build my forms. I am trying to create a page with
two separate forms. One form is for searching the database. The 2nd form is
in my table to sort the columns. As I wanted to use buttons. So I am not
100% sure if I have  gone in the right direction with my design. What I am
finding is only one occurrence of the validate_on_submit() is being set to
true from the form objects. In other words, the 2nd instance of the form on
the web page is always setting the submit validate state to True. The first
instance of the form never get set to True. I have the 2nd instance in a
sub-template. the questions I have:

1.	How can I use buttons  without using the form tag?
2.	Can I use links to perform the sort in the table and if so, how? As
I am sorting the current table. I would have to reload the page and include
the URL some form of values to indicate what field to sort on. This is the
bit I do not know how.
3.	Can you have two forms on the same page and get validate_on_submit()
methods to correctly behave as I outlined above.

Code extracts showing what is not working. View/Routes code first:

 

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

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

def index():

    tables = Accounts.query.all()

    form = SearchForm()

    sort_form = SortForm()

    print ("validate submit", sort_form.validate_on_submit(),
form.validate_on_submit())

    if form.validate_on_submit(): 

         # code here never gets run.

    elif sort_form.validate_on_submit ():

        # code here always gets run regardless what button is click.

    else:

        records = Transactions.query.order_by(Transactions.subcategory_id,
Transactions.transactiondate.desc())

    page = request.args.get('page', 1, type=int)

    records  = records.paginate(page, app.config['POSTS_PER_PAGE'], False)

    next_url = url_for('index', page=records.next_num) if records.has_next
else None

    prev_url = url_for('index', page=records.prev_num) if records.has_prev
else None

    return render_template('index.html', title='Budget Program Main Page',
records = records.items, tables = tables, account  = 'all Transactions',
prev_url = prev_url, next_url = next_url, form = form, sort_form =
sort_form)

 

Now for the form.py:

 

class SearchForm (FlaskForm):

    subcategory = SelectField('Select Sub Category', choices= [(c.id,
c.subcategory) for c in SubCategories.query.order_by('subcategory')],
default=1)

    search = StringField('Description')

    submit = SubmitField('Search')

 

 

class SortForm (FlaskForm):

    sortby_date =  SubmitField('Sort By date')

    sortby_description  = SubmitField('sort by Description')

    sortby_subcategory  = SubmitField('sort by Subcategory')

    sortby_category  = SubmitField('sort by Category')

 

 

Now for the Index file:

 

{% extends "base.html" %}

 

{% block content %}

    <h1> {{ account }}  </h1></h1>

        <form action="" method="post" novalidate>

        {{ form.hidden_tag() }}

        {{ form.subcategory.label }}

        {{ form.subcategory(rows=10) }}<br>

            {{ form.search.label }}<br>

            {{ form.search(size=64) }}<br>

            {% for error in form.search.errors %}

            <span style="color: red;">[{{ error }}]</span>

            {% endfor %}

        </p>

        <p> {{ form.submit() }}</p>

    </form>

    {% include 'tables.html' %}

{% endblock %}

 

Now for the extracted sub-template called tables:

 

 

<div class="data">

    {% if prev_url %}

        <a href="{{ prev_url }}">Previous  Records</a>

    {% endif %}

    {% if next_url %}

        <a href="{{ next_url }}">Next Records</a>

    {% endif %}

    <table id='records_table'><tbody>

        <caption> Records   </caption>

        <form action="" method="post" novalidate>

        {{ form.hidden_tag() }}

            <tr>

            <th scope='col'>Account Name</th>

            <th scope='col'>Account Number</th>

            <th scope='col'> {{ sort_form.sortby_date() }} </th>

            <th scope='col'> {{ sort_form.sortby_description () }} </th>

            <th scope='col'>Amount</th>

            <th scope='col'> {{ sort_form.sortby_category() }} </th>

            <th scope='col'> {{ sort_form.sortby_subcategory () }} </th>

            </tr>

        </form>

        {% for row in records %}

 

Any ideas? If I could use links than buttons, this would e okay. 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20190807/c8a06e39/attachment-0001.html>


More information about the Flask mailing list