Home > Enterprise >  The filter crashes when you click on another page of the flask python pagination
The filter crashes when you click on another page of the flask python pagination

Time:12-29

Please tell me how to correctly write the condition for the rejection of information. I use pagination, and it will show everything correctly, when I click on the find button, it shows me the data that is needed and the number of pages where this data is also there. But the problem is that when I click to go to the next page, it resets. And returns all the pages where everything is. How can this be fixed?

summary.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Summary</title>
    <link type="text/css" href="{{url_for('static', filename='css/summary.css')}}" rel="stylesheet"/>
    <script type="text/javascript" src="{{url_for('static', filename='js/search.js')}}"></script>
</head>
<body>
<div >
 <table >
    <tbody>
    {% for el in result.items %}
        <tr>
            <td>{{ el.source }}</td>
            <td>{{ el.object }}</td>
            <td>{{ el.hostname }}</td>
            <td>{{ el.ip_addres }}</td>
            <td>{{ el.status }}</td>
            <td>{{ el.mac_address }}</td>
            <td>{{ el.serial_number }}</td>
            <td>{{ el.worker }}</td>
            <td>{{ el.owner }}</td>
            <td>{{ el.redirect }}</td>
            <td>{{ el.plata_no_repair }}</td>
            <td>{{ el.plata_removed }}</td>
            <td>{{ el.model }}</td>
            <td>{{ el.comment }}</td>
            <td>{{ el.result_power }}</td>
        </tr>
    {% endfor %}
    </tbody>
 </table>
</div>


<div>
    {% for page in result.iter_pages() %}
        {% if page %}
            <a href="{{ url_for('summary', page=page, tag=tag) }}">{{ page }}</a>
        {% else %}
            ...
        {% endif %}
    {% endfor %}
</div>


<form method="POST" action="">
    <div >
        <h3>Search: {{ tag }} </h3>
        <h3>Global search:</h3>
        <input type="text" name="search" id="search" >
        <input  type="submit" value="Search">
    </div>
</form>
</body>
</html>

summary.py:

from flask import render_template, request
from sqlalchemy import or_

from sweater.models import Summary_all_filter
from sweater import app


@app.route('/summary', methods=['GET', 'POST'], defaults={"page": 1})
@app.route('/<int:page>', methods=['GET', 'POST'])
def summary(page):
    page = page
    pages = 15
    summary_all = Summary_all_filter.query.order_by(Summary_all_filter.source).paginate(per_page=pages, page=page, error_out=False)
    if request.method == "POST" and 'search' in request.form:
        tag = request.form["search"]
        search = "%{}%".format(tag)
        print(tag)
        if search != '%%':

            saf = Summary_all_filter.query.order_by(Summary_all_filter.source).filter(or_(Summary_all_filter.serial_number.like(search), Summary_all_filter.source.like(search), Summary_all_filter.object.like(search),
                                                                                            Summary_all_filter.hostname.like(search), Summary_all_filter.worker.like(search), Summary_all_filter.ip_addres.like(search),
                                                                                            Summary_all_filter.model.like(search), Summary_all_filter.owner.like(search), Summary_all_filter.redirect.like(search))).paginate(per_page=pages, error_out=False)
        else:
            saf = Summary_all_filter.query.order_by(Summary_all_filter.source).paginate(per_page=pages, error_out=False)

        return render_template('summary.html', result=saf, tag=search)

    return render_template('summary.html', result=summary_all)

I tried to create another page and transfer the data there, but it didn't work. My goal is to make a data search engine for all pages

CodePudding user response:

Now i see the problem.

@app.route('/summary', methods=['GET', 'POST'], defaults={"page": 1})
@app.route('/<int:page>', methods=['GET', 'POST'])
def summary(page):
    page = page
    pages = 15
    summary_all = Summary_all_filter.query.order_by(Summary_all_filter.source).paginate(per_page=pages, page=page, error_out=False)

    # Here is the error: 
    # You never do the query if comming from a GET request.
    # When you click on the next pages it emmits a GET request so it never enters here
    if request.method == "POST" and 'search' in request.form:
        tag = request.form["search"]
        search = "%{}%".format(tag)
        if search != '%%':
            saf = LONGQUERY.paginate(per_page=pages, error_out=False)
        else:
            saf = NOT_SO_LONGQUERY.paginate(per_page=pages, error_out=False)
        return render_template('summary.html', result=saf, tag=search)

    return render_template('summary.html', result=summary_all)

So now you could try something like this. I believe the result should be as expected.

@app.route('/summary', methods=['GET', 'POST'], defaults={"page": 1})
@app.route('/<int:page>', methods=['GET', 'POST'])
def summary(page):
    pages = 15

    # Check for the search form on a POST request if any or
    # Checks for a tag paramater if GET request or None
    tag = request.form.get('search') or request.args.get('tag')

    if tag is not None:
        # Correct format if comes from a POST request
        # If tag comes with %% is a GET request if not is a POST 
        page = page if tag.startswith('%') else 1
        tag = tag if tag.startswith('%') else f'%{tag}%'
        #Execute the query
        saf = LONGQUERY.paginate(per_page=pages, page=page, error_out=False)
        return render_template('summary.html', result=saf, tag=tag)

    # Any other case points to the same global query
    summary_all = Summary_all_filter.query.order_by(Summary_all_filter.source).paginate(per_page=pages, page=page, error_out=False)
    return render_template('summary.html', result=summary_all)

  • Related